Java的Thread局部变量ThreadLocal


ThreadLocal:    Thread局部变量-------ThreadLocalVariable


java.lang
Class ThreadLocal<T>
java.lang.Object
  java.lang.ThreadLocal<T>
Direct Known Subclasses:
InheritableThreadLocal
public class ThreadLocal<T>
extends Object
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread

ThreadLocal为每个线程使用该变量的线程提供一个独立的副本,这样每个副本读可以独立改变自己的副本,不受其他线程的影响

这个类有四个方法:

Method Summary

 Tget()
          Returns the value in the current thread's copy of this thread-local variable.

//返回当前线程对应的局部变量的值
protected  T initialValue()
          Returns the current thread's initial value for this thread-local variable.

      //返回该线程变量的初始值,缺省值返回null,protected修饰,便于子类覆盖,是一个延迟调用,当第一次调用set(),get()时才执行1次
 void remove()
          Removes the value for this ThreadLocal.

//删除当前局部变量的值
 void set(T value)
          Sets the current thread's copy of this thread-local variable to the specified value.

//设置当前线程局部变量的值:

相关内容

    暂无相关文章