This is the 8th day of my participation in Gwen Challenge
1. What is ThreadLocal?
The ThreadLocal and synchronized keywords are used to ensure concurrent access in multi-threaded situations, but there are essential differences between ThreadLocal and synchronized keywords
Synchronized uses a locking mechanism to allow only one thread of code to access a piece of code or variable at a time.
ThreadLocal provides a copy of a variable for each thread, so each thread has a copy of a variable, and each thread accesses its own copy of a variable. This allows for so-called thread isolation. For example, Spring uses ThreadLocal, and when you implement a transaction, The Handler Looper class takes advantage of ThreadLocal to ensure that only one Looper object exists per thread
2. Use ThreadLocal
public class UseThreadLocal {
private static ThreadLocal<Integer> threadLocal=new ThreadLocal<Integer>(){
@Override
protected Integer initialValue(a) {// Initialize variables
return 1; }};public static void main(String[] args) {
for (int i=0; i<3; i++){new Thread(newTestThread(i)).start(); }}public static class TestThread implements Runnable{
int id;
public TestThread(int id) {
this.id=id;
}
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName()+":start");
Integer s= threadLocal.get();
s=s+id;
threadLocal.set(s);
System.out.println(Thread.currentThread().getName()+":"+ threadLocal.get()); }}}Copy the code
The results
Thread0, Thread1, and Thread2 output 1,2, and 3, respectively. There is no interaction between these three threads
Let’s take a look at the effect of not using ThreadLocal
private static Integer count=new Integer(1);
public static void main(String[] args) {
for (int i=0; i<3; i++){new Thread(newTestThread(i)).start(); }}public static class TestThread implements Runnable{
int id;
public TestThread(int id) {
this.id=id;
}
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName()+":start");
count=count+id;
System.out.println(Thread.currentThread().getName()+":"+count ); }}Copy the code
The results
As a result, they are not isolated, and all three threads operate on the same object, not a copy of the object
3. Implementation of ThreadLocal
ThreadLocal looks at its implementation. Where to start? Let’s start with threadLocal’s set and GET methods
The get method
What do we see in this method?
There is a stored Map
Each get gets the ThreadLocalMap of the current thread. Let’s look at the getMap method
ThreadLocals Objects that are present in every thread
And the ThreadLocalMap is defined in a ThreadLocal and then we go down and get the map, and if it’s empty it returns the initial value, if it’s not empty it returns an Entry object with the current ThreadLocal as its Key, Then it returns the value of the Entry
Let’s go back to the Entry in the code
Emmm is the same as I said, but XDM if you look at the code up there there is an Entry array, why do you use an array? Since we might define more than just a ThreadLocal, this makes sense
Now look at setting the initial value
It stores the current ThreadLocal as the Key and the initial value as the value into the Entry array of the ThradLocalMap
The set method of ThreadLocalMap
So now you see how it works, right
Let’s look again at the set method of ThreadLocal
That’s not too different from how we set the initial value when we set the value of a ThreadLocal which is when we execute the set method, it sets the value of a ThreadLocal in the LocalMap of each thread object, This implements a copy of the variables in each thread. You can see how ThreadLocal works. If any XDM sees this and feels its head buzzing, let’s see the picture above
3, summarize
We learned about the use of ThreadLocal and how to implement it in the source code. If there are mistakes, please put forward in the comments area! Hope the big guys hit the button three times!