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 (e.g., a user id or transaction id).
for example, the class below generates unique identifiers local to each thread.
a thread's id is assigned the first time it invokes threadid.get() and
remains unchanged on subsequent calls.
import java.util.concurrent.atomic.atomicinteger;
public class threadid {
// atomic integer containing the next thread id to be assigned
private static final atomicinteger nextid = new atomicinteger(0);
// thread local variable containing each thread's id
private static final threadlocal<integer> threadid =
new threadlocal<integer>() {
@override protected integer initialvalue() {
return nextid.getandincrement();
}
};
// returns the current thread's unique id, assigning it if necessary
public static int get() {
return threadid.get();
}
}
each thread holds an implicit reference to its copy of a thread-local
variable as long as the thread is alive and the threadlocal instance is
accessible; after a thread goes away, all of its copies of thread-local
instances are subject to garbage collection (unless other references to
these copies exist).
/**
* sets the current thread's copy of this thread-local variable
* to the specified value. most subclasses will have no need to
* override this method, relying solely on the {@link #initialvalue}
* method to set the values of thread-locals.
* @param value the value to be stored in the current thread's copy of
* this thread-local.
**/
public void set(t value) {
thread t = thread.currentthread();
threadlocalmap map = getmap(t);
if (map != null)
map.set(this, value);
else
createmap(t, value);
}
/**
* get the map associated with a threadlocal. overridden in
* inheritablethreadlocal.
* @param t the current thread
* @return the map
**/
threadlocalmap getmap(thread t) {
return t.threadlocals;
}
/**
* create the map associated with a threadlocal. overridden in
* inheritablethreadlocal.
* @param t the current thread
* @param firstvalue value for the initial entry of the map
**/
void createmap(thread t, t firstvalue) {
t.threadlocals = new threadlocalmap(this, firstvalue);
}
/**
* returns the value in the current thread's copy of this
* thread-local variable. if the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialvalue} method.
* @return the current thread's value of this thread-local
**/
public t get() {
thread t = thread.currentthread();
threadlocalmap map = getmap(t);
if (map != null) {
threadlocalmap.entry e = map.getentry(this);
if (e != null) {
@suppresswarnings("unchecked")
t result = (t)e.value;
return result;
}
}
return setinitialvalue();
}
/**
* variant of set() to establish initialvalue. used instead
* of set() in case user has overridden the set() method.
* @return the initial value
**/
private t setinitialvalue() {
t value = initialvalue();
thread t = thread.currentthread();
threadlocalmap map = getmap(t);
if (map != null)
map.set(this, value);
else
createmap(t, value);
return value;
}
protected t initialvalue() {
return null;
}
文章的最后,简单介绍一下threadlocalmap这个类,该类是threadlocal的静态内部类。它对hashmap进行了改造,用于保存各个threadlocal变量和某线程的该变量的值的映射关系。每个线程都有一个threadlocalmap类型的属性。threadlocalmap中的table数组的长度,与该线程访问的threadlocal类型变量的个数有关,而与别的无关。
this is the end。 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/li_canhui/article/details/85231116