As early as Java 1.2 is launched, a new support is introduced in the Java platform: java.lang.threadlocal, gives us a new option when writing multithreaded programs. Using this tool class can be very simple to write beautiful multi-threaded programs, although Threadlocal is very useful, it seems to be understood now, and there are not many friends using it. What is Threadlocal what threadlocal? In fact, Threadlocal is not a remote version of a thread, which is not a Thread, but thread local variable (thread local variable). Maybe named it to Threadlocalvar is more appropriate. Threadlocal actually function is very simple, which provides a copy of a variable value for each thread using the variable, and every thread can independently change its own copy, not a copy of other threads. conflict. From the perspective of the thread, it seems that each thread has this variable. Thread local variables are not Java's new invention, and in other language compilers (such as IBM XL Fortran), it provides direct support in the language level. Because Java did not provide direct support in the language level, but provided a THReadLocal class to provide support, so the code to write the wires in Java is relatively awkward, which may be that the thread local variable is not available in Java. A reason for a very good popularity. Threadlocal's design first looks at Threadlocal interface: Object get (); // Returns the thread local variable copy protected object initialvalue (); // Returns the initial value of the current thread of the thread part VoID Set (Object Value) / / Set the value Threadlocal of the thread local variable copy of the current thread has three methods, which is worth noting the initialvalue (), which is a method of protected, apparently implemented for subclass rewriting. This method returns the initial value of the current thread at the primary variable of the thread, which is a delay calling method, and is executed when a thread is first calling GET () or set (Object), and only once. The real implementation in Threadlocal returns a NULL:
Protected Object InitialValue () {RETURN NULL;} How to maintain a copy of the variable for each thread? In fact, the idea of achieving is very simple, there is a map in the Threadlocal class, which is used to store copies of the variables of each thread. For example, the following example implementation:
public class ThreadLocal {private Map values = Collections.synchronizedMap (new HashMap ()); public Object get () {Thread curThread = Thread.currentThread (); Object o = values.get (curThread); if (o == null && ! VALUES.CONTAINSKEY () {o = initialvalue (); values.put (curnetread, o);} returno} public void set (ibject newvalue) {VALUES.PUT (thread.currentthread (), newValue; } Public object initialvalue () {return null;}} Of course, this is not an industrial intensity, but the overall idea of Threadlocal in JDK is also similar to this. Threadlocal Use If you want thread local variables to initialize other values, you need to implement Threadlocal subclass and rewrite this method, usually use an internal anonymous class to subclassify Threadlocal, such as the following example, serialnum class for each class assigned a number: public class SerialNum {// The next serial number to be assigned private static int nextSerialNum = 0; private static ThreadLocal serialNum = new ThreadLocal () {protected synchronized Object initialValue () {return new Integer (nextSerialNum );}} Public static int GET () {Return ((Integer)))). INTVALUE ();}} The use of the Serialnum class is very simple, because the get () method is static, so you need to get When the serial number of the current thread is simply called: