当前位置:网站首页>Multithread 07 -- ThreadLocal
Multithread 07 -- ThreadLocal
2022-07-22 20:54:00 【fengxianaa】
Last one :https://blog.csdn.net/fengxianaa/article/details/124427303
1. Easy to use
/**
* ThreadLocal
* seeing the name of a thing one thinks of its function : Thread local variable , Realize data isolation between threads , Thread game over , The set variables will also be recycled
*
*/
public class ThreadLocal01 {
static ThreadLocal myThreadLocal = new ThreadLocal();
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
myThreadLocal.set("feng");// At this time ,"feng" Associated with the current thread , Private variables belonging to the current thread , Can only get in the current thread
System.out.println(Thread.currentThread().getName()+"----"+myThreadLocal.get());
ThreadHelper.sleep(2000);
});
t.start();
System.out.println(myThreadLocal.get());// main Cannot get in the thread
}
}
2. Use scenarios
/**
* Use scenarios
* 1. Parameter passing
*
*/
public class ThreadLocal02 {
void save(Object Person){
/**
* TODO This method is used in business , Use the current login user (currentUser), If you add parameters , Don't fit , reason :
* 1. In the microservices architecture , The interface is external , If you modify it at will , It may cause external service error
* 2. If you add parameters , It will cause ambiguity in the method
* 3. If it's time to service Many methods need currentUser, Then add it all , Obviously unrealistic
*
* TODO You can use it at this point Threadlocal, At the interceptor level , hold currentUser Set to ThreadLocal in ,
*
*/
}
}
3. ThreadLocalMap structure
ThreadLocal The essence is to operate the current thread theadlocals attribute , It is ThreadLocalMap type
ThreadLocalMap structure : Storage Entry Array , The element in the array has two attributes :referent、value
- referent : yes ThreadLocal object
- value: Call yourself ThreadLocal Of set Method , Set the value of the
TheadLocal Follow threadLocals The relationship between
Now you can see myThreadLocal The object that the variable points to is 672
View the of the current thread threadLocals attribute
You can see , There is one on the innermost layer referent Attributes also point to 672
Actually set The essence : hold myThreadLocal Objects and feng, Encapsulate into a Entry object , Then put it in the current thread threadLocals Properties of the
4. principle
/**
* ThreadLocal principle
*
*/
public class ThreadLocal03 {
static ThreadLocal myThreadLocal = new ThreadLocal();
public static void main(String[] args) {
Thread t = new Thread(() -> {
myThreadLocal.set("feng");// At this time ,"feng" Associated with the current thread , Private variables belonging to the current thread , Can only get in the current thread
System.out.println(Thread.currentThread().getName()+"----"+myThreadLocal.get());
});
t.start();
/**
* TODO
* set Methods to introduce , Essentially, the operation is the current thread object threadLocals attribute
* 1. obtain currentThread Of threadLocals attribute , yes ThreadLocalMap type
* 2. threadLocals Is it null
* yes :new ThreadLocalMap(myThreadLocal,"feng")
* stay ThreadLocalMap In the construction method of , Created a length of 16 Of Entry Array , And assign it to table attribute
* utilize myThreadLocal Of threadLocalHashCode Attribute calculates a subscript i
* table[i] = new Entry(myThreadLocal, "feng");
* Entry Is a weak reference
* no : utilize myThreadLocal Of threadLocalHashCode Attribute calculates a subscript i
* Take out Entry object , then entry.value="feng"
*
*/
}
}
5. Reference type
/**
* ThreadLocal
*
*/
public class ThreadLocal03 {
static ThreadLocal myThreadLocal = new ThreadLocal();
public static void main(String[] args) {
Thread t = new Thread(() -> {
myThreadLocal.set("feng");// At this time ,"feng" Associated with the current thread , Private variables belonging to the current thread , Can only get in the current thread
System.out.println(Thread.currentThread().getName()+"----"+myThreadLocal.get());
});
t.start();
/**
* TODO
* strong : commonly A a = new A() , Is strong
* weak :WeakReference ,gc It will be recycled when it happens
* soft :SoftReference,A a = new A();SoftReference<A> soft = new SoftReference<>(a);
* soft It's possible that gc Recycling , But it's happening OOM Before , Will be recycled
* Some useful but not necessary objects can be soft referenced
* virtual :PhantomReference, Also called ghost quotation , You will receive a notice when it is recycled
*
*/
}
}
6. Memory leak
call set After the method , Set up myThreadLocal=null And currentThread It's not over
At this time , cannot access currentThread Of threadLocals attribute , The set value will never be obtained , Still in memory
/**
* ThreadLocal
*
*/
public class ThreadLocal03 {
static ThreadLocal myThreadLocal = new ThreadLocal();
public static void main(String[] args) {
Thread t = new Thread(() -> {
myThreadLocal.set("feng");// At this time ,"feng" Associated with the current thread , Private variables belonging to the current thread , Can only get in the current thread
System.out.println(Thread.currentThread().getName()+"----"+myThreadLocal.get());
});
t.start();
/**
* TODO Memory leak
* call set After the method ,myThreadLocal=null And currentThread It's not over ,
* At this time , cannot access currentThread Of threadLocals attribute , The set value will never be obtained , Still in memory
*/
}
}
hold myThreadLocal Set to null after , You can see referent Nothing has changed
But in execution System.gc(); After this code
referent become null, So at this point ,feng This value , You'll never get it again
Of course , After the current thread ends feng This value will still be recycled
边栏推荐
猜你喜欢
随机推荐
locust测试框架快速搭建
Data skew
多线程01--创建线程和线程状态
线程池01--基础使用
寄生感知共质心二进制加权电容器布局生成集成了布局、布线和单元电容器尺寸
New attribute of class (elementary understanding)
Opportunity of code 2020 -- boarding seat problem
scanf和printf格式问题
初次见面 多多关照
Parasitic sensing size and detailed routing of binary weighted capacitors in charge graded DAC
Servlet
Redis series 12 -- redis master-slave
Redis 系列11--Redis 持久化
Pycharm settings
Automatic current mirror layout (acml) tool
Common centroid layout of active and passive equipment: review and future road
C language (Itoa function)
Common centroid capacitor layout generation considering device matching and parasitic minimization
【面试:基础篇04:插入排序】
1076 forwards on Weibo (30 points)