实战十二:threadlocal 使用
ThreadLocal是如何做到为每一个线程维护变量的副本的呢?其实实现的思路很简单:在ThreadLocal类中有一个Map,用于存储每一个线程的变量副本,Map中元素的键为线程对象,而值对应线程的变量副本。我们自己就可以提供一个简单的实现版本
public class TestThreadLocal {
ThreadLocal<String> myLocal = new ThreadLocal<String>();
String myName;
public String getName() {
return myName;
}
public void setName(String name) {
myName = name;
}
public static void main(String[] args) throws InterruptedException {
TestThreadLocal ooo = new TestThreadLocal();
Thread[] threadArray = new Thread[10];
for (int i = 0; i < 10; i++) {
Thread thi = new Thread(new ThreadWithoutThreadLocal(ooo), "thread" + i);
threadArray[i] = thi;
}
for (Thread th : threadArray) {
th.start();
}
Thread.sleep(3000);
System.out.println("-----------------------------------------------------------------");
for (int i = 0; i < 10; i++) {
Thread thi = new Thread(new ThreadWithThreadLocal(ooo), "thread" + i);
threadArray[i] = thi;
}
for (Thread th : threadArray) {
th.start();
}
}
}
class ThreadWithoutThreadLocal implements Runnable {
public TestThreadLocal ooo;
public ThreadWithoutThreadLocal(TestThreadLocal o) {
ooo = o;
}
@Override
public void run() {
String localName = Thread.currentThread().getName();
ooo.setName(localName);
ooo.myLocal.set(localName);
// it will be mess up if multi thread running
// result like this
// ThreadName =======thread1 but it's actual name thread1
// ThreadName =======thread2 but it's actual name thread2
// ThreadName =======thread1 but it's actual name thread0
// ThreadName =======thread4 but it's actual name thread4
// ThreadName =======thread3 but it's actual name thread3
// ThreadName =======thread7 but it's actual name thread7
// ThreadName =======thread6 but it's actual name thread6
// ThreadName =======thread5 but it's actual name thread5
// ThreadName =======thread8 but it's actual name thread8
// ThreadName =======thread9 but it's actual name thread9
System.out.println(
"ThreadName =======" + ooo.getName() + " but it's actual name " + Thread.currentThread().getName());
}
}
class ThreadWithThreadLocal implements Runnable {
public TestThreadLocal ooo;
public ThreadWithThreadLocal(TestThreadLocal o) {
ooo = o;
}
@Override
public void run() {
String localName = Thread.currentThread().getName();
ooo.myLocal.set(Thread.currentThread().getName());
// ThreadName======thread0 but it's actual name thread0
// ThreadName======thread2 but it's actual name thread2
// ThreadName======thread1 but it's actual name thread1
// ThreadName======thread3 but it's actual name thread3
// ThreadName======thread5 but it's actual name thread5
// ThreadName======thread4 but it's actual name thread4
// ThreadName======thread8 but it's actual name thread8
// ThreadName======thread6 but it's actual name thread6
// ThreadName======thread9 but it's actual name thread9
// ThreadName======thread7 but it's actual name thread7
System.out.println("ThreadName======" + ooo.myLocal.get() + " but it's actual name " + localName);
}
}
阅读更多- 并发编程实战 1.8. 线程中变量的使用 - ThreadLocal
- 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序
- 机器学习实战笔记(十二):使用 Apriori 算法进行关联分析
- kubernetes实战(十二):k8s使用helm持久化部署redmine集成openLDAP
- 网络层架构设计与实战十二框架重构之NiceClient封装
- GCD实战一:使用串行队列实现简单的预加载
- Java线程(十二)----线程本地变量ThreadLocal
- Docker实战:使用Dockerfile创建带Apache服务并支持ssh的Centos Docker镜像
- Go实战--net/http中JSON的使用(The way to go)
- Android之使用Android-query框架开发实战(一)
- 【Composer】实战操作一:使用库
- 【软件性能测试-LoadRunner实战技能 3】== LR所使用的脚本语言初解
- Android tips(十二)-->Android开发中使用矢量图
- 【软件自动化测试-QTP实战技能 7】== QTP中类的基本使用方法
- 实战:ORACLE SQL Performance Analyzer的使用
- ThreadLocal使用介绍
- 通向架构师的道路(七)--之漫谈使用ThreadLocal改进你的层次的划分
- iOS项目开发实战——使用同步请求获取网页源码
- 【java项目实战】一步步教你使用MyEclipse搭建java Web项目开发环境(一)
- 项目实战之使用Servlet+JSP+XML(EL表达式版)实现联系人功能