
Java中ThreadLocal浅析
发布日期:2021-05-27 02:54:23
浏览次数:5
分类:技术文章
本文共 5199 字,大约阅读时间需要 17 分钟。
ThreadLocal是线程内的全局上下文。就是在单个线程中,方法之间共享的内存,每个方法都可以从该上下文中获取值和修改值。
在业务开发中,ThreadLocal 有两种典型的使用场景
- ThreadLocal 用作保存每个线程独享的对象,为每个线程都创建一个副本,这样每个线程都可以修改自己所拥有的副本, 而不会影响其他线程的副本,确保了线程安全。
- ThreadLocal 用作每个线程内需要独立保存信息,以便供其他方法更方便地获取该信息的场景。每个线程获取到的信息可能都是不一样的,前面执行的方法保存了信息后,后续方法可以通过ThreadLocal 直接获取到,避免了传参,类似于全局变量的概念。
场景1
public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { int finalI = i; new Thread(() -> { String data = new Demo().date(finalI); System.out.println(data); }).start(); Thread.sleep(100); } } private String date(int seconds){ Date date = new Date(1000 * seconds); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss"); return simpleDateFormat.format(date); }
打印
00:00
00:01 00:02 00:03 00:04 00:05 00:06 00:07 00:08 00:09给每个线程都创建了SimpleDateFormat对象,他们之间互不影响,代码是可以正常执行的。
如果有1000个线程都用到SimpleDateFormat对象呢,对这种场景,ThreadLocal再合适不过了,ThreadLocal给每个线程维护一个自己的simpleDateFormat对象,这个对象在线程之间是独立的,互相没有关系的。这也就避免了线程安全问题。与此同时,simpleDateFormat对象还不会创造过多,线程池一共只有 16 个线程,所以需要16个对象即可。
public static ExecutorService threadPool = Executors.newFixedThreadPool(16); public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 1000; i++) { int finalI = i; threadPool.submit(() -> { String data = new Demo().date(finalI); System.out.println(data); }); } threadPool.shutdown(); } private String date(int seconds){ Date date = new Date(1000 * seconds); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss"); ThreadLocalUtil.set("dateFormat",simpleDateFormat); SimpleDateFormat dateFormat = ThreadLocalUtil.get("dateFormat"); return dateFormat.format(date); }
场景2
传统方式我们要在方法中访问某个变量,可以通过传参的形式往方法中传参,如果多个方法都要使用那么每个方法都要传参;如果使用ThreadLocal所有方法就不需要传该参数了,每个方法都可以通过ThreadLocal来访问该值。
public class ThreadLocalDemo { public static void main(String[] args) throws InterruptedException { //User user = new User("曹操"); //new Service1().service1(user); for (int i = 0; i < 10; i++) { int m = i; new Thread(() -> { User user = new User("曹操" + m); new Service1().service1(user); }).start(); Thread.sleep(100); } }}class Service1 { public void service1(User user){ //给ThreadLocal赋值,后续的服务直接通过ThreadLocal获取就行了 ThreadLocalUtil.set("user",user); new Service2().service2(); }}class Service2 { public void service2(){ User user = ThreadLocalUtil.get("user"); System.out.println("service2拿到的用户:"+user.name); new Service3().service3(); }}class Service3 { public void service3(){ User user = ThreadLocalUtil.get("user"); System.out.println("service3拿到的用户:"+user.name); //在整个流程执行完毕后,一定要执行remove ThreadLocalUtil.remove(); }}class User { String name; public User(String name){ this.name = name; }}public class ThreadLocalUtil{ private static final ThreadLocal
打印
service2拿到的用户:曹操0
service3拿到的用户:曹操0 service2拿到的用户:曹操1 service3拿到的用户:曹操1 service2拿到的用户:曹操2 service3拿到的用户:曹操2 service2拿到的用户:曹操3 service3拿到的用户:曹操3 service2拿到的用户:曹操4 service3拿到的用户:曹操4 service2拿到的用户:曹操5 service3拿到的用户:曹操5 service2拿到的用户:曹操6 service3拿到的用户:曹操6 service2拿到的用户:曹操7 service3拿到的用户:曹操7 service2拿到的用户:曹操8 service3拿到的用户:曹操8 service2拿到的用户:曹操9 service3拿到的用户:曹操9
转载地址:https://blog.csdn.net/leijie0322/article/details/113861990 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2023年10月30日 19时54分16秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
nest.js实战之模块路由前缀
2019-03-25
node.js入门之Buffer字节缓冲器使用
2019-03-25
es6入门之TypedArray定型数组
2019-03-25
MXNet入门之线性回归的简洁实现
2019-03-25
express之application.js
2019-03-25
express之express.js
2019-03-25
express之全局中间件init
2019-03-25
express之全局中间件query
2019-03-25
express之request.js
2019-03-25
express之response.js
2019-03-25
express之view.js
2019-03-25
express之router
2019-03-25
express之route.js
2019-03-25
express之layer.js
2019-03-25
express内部Layer结构
2019-03-25
typeorm之Entity Listeners
2019-03-25
typeorm之subscriber
2019-03-25
typeorm插入之save与insert
2019-03-25
typeorm更新之save与update、updateById
2019-03-25
typeorm删除之remove与delete
2019-03-25