1. Brief introduction of Threadlocal
Threadlocal instances are typically present in a class as static private static fields that are used to associate a thread. ThreadLocal is a thread-level local variable, and the following is a key point for thread-local variables (ThreadLocal variables):
A, when using threadlocal to maintain variables, if multiple threads access the Threadlocal instance, Threadlocal provides a separate copy of the variable for each thread that uses the variable, so each thread can independently change its own copy. It does not affect the replicas of other threads.
B, from the thread point of view, the target variable is like a local variable of the thread, which is also the meaning of the name of the class to be expressed locally.
2. Look at threadlocal
The Threadlocal<t> class is simple, with only four methods:
(1) void set (T value), which is used to set a copy of a variable in the current thread
(2) public T get (), which is used to get a copy of the variable that threadlocal holds in the current thread
(3) public void Remove (), which is used to remove a copy of a variable in the current thread in order to reduce memory usage, which is a new method of JDK 5.0. It should be noted that when the thread ends, the local variables of the corresponding thread are automatically garbage collected, so it is not necessary to explicitly call the method to clear the thread's local variables, but it can speed up the memory reclamation.
(4) Protected T InitialValue (), this method is a protected method, which is generally used to rewrite when used, it is a lazy loading method, the default implementation in threadlocal directly returns a null.
3.ThreadLocal Example
The simple way to use it is as follows:
Packagecom.test; Public classThreadMain {//① Specifies the initial value by overwriting the Threadlocal InitialValue () method with an anonymous inner class private static threadlocal<integer> seqNum = new Threadlocal<integer >() { PublicInteger initialvalue () {return0; } }; //② getting the next sequence value Public intGetnextnum () {Seqnum.set (Seqnum.get ()+ 1); returnSeqnum.get (); } Public Static voidMain (string[] args) {ThreadMain sn=NewThreadMain (); //③ 3 threads sharing sn, each generating serial numbertestclient T1 =Newtestclient (SN); TestClient T2=Newtestclient (SN); testclient T3=Newtestclient (SN); T1.start (); T2.start (); T3.start (); } Private Static classTestClientextendsThread {PrivateThreadMain SN; Publictestclient (ThreadMain sn) { This. SN =SN; } Public voidrun () { for(inti = 0; I < 3; i++) { //④ 3 sequence values per threadSystem.out.println ("thread[" +Thread.CurrentThread (). GetName ()+ "]-sn[" +Sn.getnextnum ()+ "]"); } } } }
The results are as follows:
THREAD[THREAD-0]-sn[1]thread[thread-2]-sn[1]thread[thread-1]--sn[1]thread[ Thread-2]-sn[2]thread[thread-0]-sn[2]thread[thread-2]--sn[3]thread[ Thread-1]-sn[2]thread[thread-1]-sn[3]thread[thread-0]--sn[3]
4. Implementation mechanismof threadlocal
This section is not in-depth study, for more information, please refer to http://www.importnew.com/17849.html
(1) The Get () method source code is as follows:
5. Summary
Threadlocal is generally declared in static variables, and if you create threadlocal continuously without calling its Remove method, it will cause a memory leak, especially in high concurrency web containers.
Threadlocal is easier and more convenient than the synchronized synchronization mechanism to solve thread-safety problems when dealing with local variables of a thread, and the program has higher concurrency.
Java Concurrency: The threadlocal of thread synchronization mechanism