博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于ThreadLocal的浅谈
阅读量:4036 次
发布时间:2019-05-24

本文共 4318 字,大约阅读时间需要 14 分钟。

以前用ThreadLocal用的比较少,今天在看以前的代码的时候,发现有个类里面的方法里面使用了公共的静态变量,而且有很多类都引用了这个方法。由于这个变量不是final的,是动态赋值的,所以在多线程的情况下可能会引起问题。因为要改造这个方法的这个变量问题,所以我想到了ThreadLocal,并对ThreadLocal进行了一次实验,通过实验,对ThreadLocal有了一次比较深刻的认识。在此把代码贴出来,跟各位共同学习一下。

public class Test {    public static ThreadLocal
threadLocal = new ThreadLocal
(); private static final long waitTime = 3000L; public static void main(String[] args) throws Exception { Thread.sleep(60000); Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----111111111"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user = new User("张三"); user.say(); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----22222222"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user = new User("李四"); user.say(); } }); Thread thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----3333333333"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user1 = new User("王五"); user1.say(); User user2 = new User("赵六"); user2.say(); } }); thread1.start(); thread2.start(); thread3.start(); Thread thread4 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----4444444444"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new Runnable() { @Override public void run() { User user1 = new User("宋八1"); user1.say(); threadLocal.set(Thread.currentThread().getName()+"----5555555555"); User user2 = new User("宋八2"); user2.say(); } }).start(); new Thread(new Runnable() { @Override public void run() { User user1 = new User("钱九1"); user1.say(); threadLocal.set(Thread.currentThread().getName()+"----666666666666666"); User user2 = new User("钱九2"); user2.say(); } }).start(); } }); thread4.start(); Thread.sleep(60000); }

}

public class User {    private String name;    public User(){        super();    }    public User(String name){        this.name = name;    }    public void say(){            System.out.println(name + " say:" + Test.threadLocal.get());    }

}

结果输出如下:

Thread-2

Thread-3
Thread-4
Thread-5
张三 say:Thread-2----111111111
李四 say:Thread-3----22222222
王五 say:Thread-4----3333333333
赵六 say:Thread-4----3333333333
钱九1 say:null
钱九2 say:Thread-7----666666666666666
宋八1 say:null
宋八2 say:Thread-6----5555555555

 

做一下说明:

我创建了四个主线程,每个线程内部对ThreadLocal变量进行赋值,并在主线程内创建User对象,User对象会输出ThreadLocal的值。通过结果可以看出:

1、各个线程内部的ThreadLocal是不相互影响的,并且主线程与子线程是不相互影响的。 

2、同一个线程内容,不同对象获取的同一个ThreadLocal对象是一样的。

3、子线程是无法获取到主线程的ThreadLocal值的

通过以上试验,我还进行一下内存的监测,发现一个线程调用结束之后,会进行对象的回收。如图:

 

通过

 

以上的试验,希望对大家理解和使用ThreadLocal能有所帮助。

 

 

转载地址:http://mmcdi.baihongyu.com/

你可能感兴趣的文章
STL::deque以及由其实现的queue和stack
查看>>
WAV文件解析
查看>>
DAC输出音乐2-解决pu pu 声
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>
Win10+VS+ESP32环境搭建
查看>>
Ubuntu+win10远程桌面
查看>>
flutter-实现圆角带边框的view(android无效)
查看>>
android 代码实现圆角
查看>>
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>