目录

void set(T value)

描述 (Description)

java.lang.ThreadLocal.set()方法将此线程局部变量的当前线程副本设置为指定值。

声明 (Declaration)

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

public void set(T value)

参数 (Parameters)

value - 这是存储在当前线程的本地线程副本中的值。

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.ThreadLocal.set()方法的用法。

package com.iowiki;
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(100);
      // returns the current thread's value of this thread-local
      System.out.println("value = " + tlocal.get());
      tlocal.set(0);
      // returns the current thread's value of this thread-local
      System.out.println("value = " + tlocal.get());
   }
} 

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

value = 100
value = 0
↑回到顶部↑
WIKI教程 @2018