Java5之后,为ThreadLocal类增加了泛型支持,即ThreadLocal<T>
通过使用ThreadLocal类可以简化多线程编时的并发访问,使用这个工具类可以很简洁的隔离多线程程序的竞争资源。
实现方法:通过为每一个使用该变量的线程都提供一个变量值的副本。使得每一个线程都可以独立的改变自己的副本,而不会和其他线程的副本发生冲突。
有如下方法:
T | get()
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.
|
void | remove()
Removes the current thread's value for this thread-local variable.
|
void | set(T value)
Sets the current thread's copy of this thread-local variable to the specified value.
|