博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThreadLocal类及应用技巧; 实现线程范围内数据共享
阅读量:4151 次
发布时间:2019-05-25

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

public class ThreadTest6 {		private static ThreadLocal
currentLocalThread = new ThreadLocal
(); private static ThreadLocal
myThreadScopeData = new ThreadLocal
(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread( new Runnable() { @Override public void run() { int data = new Random().nextInt(); System.out.println(Thread.currentThread().getName()+ " has put data : " + data); currentLocalThread.set(data); //获取与本线程相关的实例 MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); myData.setName("name" + data); myData.setAge(data); //存入当前线程中 myThreadScopeData.set(myData); new A().get(); new B().get(); } } ).start(); } } static class A{ public void get(){ //int data = localThreadMap.get(Thread.currentThread()); int data = currentLocalThread.get(); System.out.println("A from " + Thread.currentThread().getName() + " get data :" + data); //获取与本线程相关的实例 //MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); //从当前线程中获取 MyThreadScopeData myData = myThreadScopeData.get(); System.out.println("A from " + Thread.currentThread().getName() + " getName :" + myData.getName() + " , getAge : " + myData.getAge()); } } static class B{ public void get(){ //int data = localThreadMap.get(Thread.currentThread()); int data = currentLocalThread.get(); System.out.println("B from " + Thread.currentThread().getName() + " get data :" + data); //获取与本线程相关的实例 //MyThreadScopeData myData = MyThreadScopeData.getThreadInstance(); //从当前线程中获取 MyThreadScopeData myData = myThreadScopeData.get(); System.out.println("B from " + Thread.currentThread().getName() + " getName :" + myData.getName() + " , getAge : " + myData.getAge()); } }}
class MyThreadScopeData{		private MyThreadScopeData(){}		/**	 * 单例	 * @return	 */	public static /*synchronized*/ MyThreadScopeData getThreadInstance(){		MyThreadScopeData instance = map.get();		if (null == instance) {			instance = new MyThreadScopeData();			map.set(instance);		}				return instance;	}		private static ThreadLocal
map = new ThreadLocal
(); private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}

 

 

打印结果:

Thread-1 has put data : -1091157391

Thread-0 has put data : -1297198387

A from  Thread-1 get data :-1091157391

A from  Thread-1 getName :name-1091157391 , getAge : -1091157391

A from  Thread-0 get data :-1297198387

A from  Thread-0 getName :name-1297198387 , getAge : -1297198387

B from  Thread-0 get data :-1297198387

B from  Thread-0 getName :name-1297198387 , getAge : -1297198387

B from  Thread-1 get data :-1091157391

B from  Thread-1 getName :name-1091157391 , getAge : -1091157391

 

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

你可能感兴趣的文章
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>
第三方SDK:讯飞语音听写
查看>>
第三方SDK:JPush SDK Eclipse
查看>>
第三方开源库:imageLoader的使用
查看>>
自定义控件:飞入飞出的效果
查看>>
自定义控件:动态获取控件的高
查看>>