ThreadLocal作用:
1.与当前线程绑定,存储当前线程的变量副本,只要是该线程未结束,都可以通过get方法获取到变量;各线程有互相隔离;
2.可以达到线程安全的效果,避免同步带来的性能消耗。
简单的小例子:
/**
*
* ThreadLocal使用案例一
* 模拟线程数据隔离
* @author Kevin
*/
public class ThreadLocalDemo1 {
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
public static void main(String[] args) {
Thread t1 = new Thread(new MyRequest(true));
t1.start();
Thread t2 = new Thread(new MyRequest(true));
t2.start();
Thread t3 = new Thread(new MyRequest(false));
t3.start();
}
static class MyRequest implements Runnable {
private boolean setThreadLocal;
MyRequest(boolean setThreadLocal){
this.setThreadLocal = setThreadLocal;
}
@Override
public void run(){
try {
for(int i=0;i<5;i++){
if(setThreadLocal) {
threadLocal.set(i);
}
System.out.println("this current thread name is :" + Thread.currentThread().getName() + "[" + threadLocal.get() + "]");
}
}finally {
/**
* 这里把ThreadLocal定义为static还有一个好处就是,由于ThreadLocal有强引用在,
* 那么在ThreadLocalMap里对应的Entry的键会永远存在,那么执行remove的时候就可以正确进行定位到并且删除!!!
*/
threadLocal.remove();
}
}
}
}
结果:
D:\Java\jdk1.8.0_131\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2019.1.1\lib\idea_rt.jar=53791:D:\JetBrains\IntelliJ IDEA Community Edition 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_131\jre\lib\charsets.jar;D:\Java\jdk1.8.0_131\jre\lib\deploy.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_131\jre\lib\javaws.jar;D:\Java\jdk1.8.0_131\jre\lib\jce.jar;D:\Java\jdk1.8.0_131\jre\lib\jfr.jar;D:\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_131\jre\lib\jsse.jar;D:\Java\jdk1.8.0_131\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_131\jre\lib\plugin.jar;D:\Java\jdk1.8.0_131\jre\lib\resources.jar;D:\Java\jdk1.8.0_131\jre\lib\rt.jar;D:\idea_workspace\july-project\java-base\target\classes;D:\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;D:\repository\junit\junit\4.12\junit-4.12.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.base.thread.ThreadLocalDemo1
this current thread name is :Thread-1[0]
this current thread name is :Thread-2[null]
this current thread name is :Thread-2[null]
this current thread name is :Thread-2[null]
this current thread name is :Thread-2[null]
this current thread name is :Thread-0[0]
this current thread name is :Thread-2[null]
this current thread name is :Thread-1[1]
this current thread name is :Thread-0[1]
this current thread name is :Thread-0[2]
this current thread name is :Thread-1[2]
this current thread name is :Thread-0[3]
this current thread name is :Thread-0[4]
this current thread name is :Thread-1[3]
this current thread name is :Thread-1[4]
Process finished with exit code 0
待补充完善:
- 线程池下的ThreadLocal使用思考,remove的重要性
- 深层剖析Thread ThreadLocalMap ThreadLocal关系
- ThreadLocal get和set方法
- 应用案例分析