The following is transferred from http://ifeve.com/java-theadlocal/:
The Threadlocal class in Java allows you to create variables that are read and written only by the same thread. So, although there are two threads executing the same code at the same time, and this code has a reference to the same threadlocal variable, the two threads still cannot see each other's threadlocal variable fields.
1. Create a Threadlocal object
Create a threadlocal variable, as shown below:
Private New ThreadLocal ();
You instantiate a Threadlocal object. Each thread needs to be instantiated only once. Although different threads access the same threadlocal variable while executing the same piece of code, each thread sees only the private threadlocal instance. So different threads don't see each other's changes when they set different values for the Threadlocal object.
2. Access Threadlocal objects
Once you have created a Threadlocal object, you can store the value of this object in the following ways:
Mythreadlocal.set ("A thread Local value");
You can also read the value of a Threadlocal object directly:
String threadlocalvalue = (string) mythreadlocal.get ();
The Get () method returns an object, and the set () method relies on an object parameter.
3, threadlocal generic type
In order for the Get () method to return a value without forcing a type conversion, you can typically create a generic threadlocal object. Here is an example of a generic threadlocal:
Private New Threadlocal<string> ();
Now you can store a string into the threadlocal instance, and when you get the value from this threadlocal instance, you don't have to do a forced type conversion.
Mythreadlocal1.set ("Hello ThreadLocal"= Mythreadlocal.get ();
4. Initialize Threadlocal
Because the value of the set () method of the Threadlocal object is only visible to the current thread, there is a way to make the value set for the Threadlocal object visible to all threads.
To do this, we can specify an initialization value for the Threadlocal object by threadlocal the implementation of the subclass and by covering the InitialValue () method. As shown below:
Private New Threadlocal<string>() { @Override protected String InitialValue () { Return "This is the initial value"; };
At this point, before the set () method call, when the Get () method is called, all threads can see the same initialization value.
5, full ThreadLocal Example
The following is an example of a complete threadlocal:
Public classThreadlocalexample { Public Static classMyrunnableImplementsRunnable {PrivateThreadlocal<integer> ThreadLocal =NewThreadlocal<integer>(); @Override Public voidrun () {Threadlocal.set (int) (Math.random () *100D)); Try{Thread.Sleep (2000); } Catch(Interruptedexception e) {} System.out.println (Threadlocal.get ()); } } Public Static voidMain (string[] args)throwsinterruptedexception {myrunnable sharedrunnableinstance=Newmyrunnable (); Thread Thread1=NewThread (sharedrunnableinstance); Thread thread2=NewThread (sharedrunnableinstance); Thread1.start (); Thread2.start (); Thread1.join (); //wait for the thread 1 to terminateThread2.join ();//wait for the thread 2 to terminate }}
Two threads are created above to share a myrunnable instance. When each thread executes the run () method, it sets a different value for the same threadlocal instance. If the set () method is called with the Synchronized keyword and is not an Threadlocal object instance, the second thread overrides the value set by the first thread.
However, because it is a Threadlocal object, two threads cannot see each other's values. Therefore, you can set or get different values.
6, Inheritablethreadlocal
The Inheritablethreadlocal class is a subclass of Threadlocal. To resolve that each thread inside the Threadlocal instance sees only its own private value, Inheritablethreadlocal allows all child threads created by a thread to access the value of its parent thread.
Test Project: Https://github.com/easonjim/5_java_example/tree/master/javathread/test3
14. Java Concurrency and multithreading-java ThreadLocal