Java.lang.ThreadLocal.remove() 方法

描述

java.lang.ThreadLocal.remove() 方法为这个线程局部变量移除当前线程的值。


声明

以下是 java.lang.ThreadLocal.remove() 方法的声明。

public void remove()

参数

NA


返回值

此方法不返回任何值。


异常

NA


示例

下面的例子展示了 java.lang.ThreadLocal.remove() 方法的使用。

package com.tutorialspoint;

import java.lang.*;

public class ThreadLocalDemo {

   public static void main(String[] args) {

      ThreadLocal<Integer> tlocal = new ThreadLocal<Integer>();  

      /* sets the current thread's copy of this thread-local variable
         to the specified value. */

      tlocal.set(50);
      // returns the current thread's value of this thread-local
      System.out.println("value = " + tlocal.get());
 
      // removes the current thread's value 
      tlocal.remove();
      // returns the current thread's value of this thread-local
      System.out.println("value = " + tlocal.get());
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

value = 50
value = null