多线程编程入门(8):线程范围内的共享变量之ThreadLocal
2016-07-31 00:06
417 查看
package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.Random; //ThreadLocal方式实现线程范围内的共享变量 public class ThreadLocalTest { private static Map<Thread,Integer> threadData = new HashMap<Thread,Integer>();//线程内的数据 private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();//ThreadLocal方式实现线程内的数据共享(Integer) private static ThreadLocal<Person> threadLocalPerson = new ThreadLocal<Person>();//ThreadLocal方式实现线程内的数据共享(Person对象) 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); //threadData.put(Thread.currentThread(), data);//线程内的数据 threadLocal.set(data);//ThreadLocal方式实现线程内的数据共享 //Person person = new Person(); //person.setName("zhangsan_"+Thread.currentThread()); //person.setAge(data); //threadLocalPerson.set(person); Person.getThreadLocalInstance().setName("name"+data); Person.getThreadLocalInstance().setAge(data); new A().get(); new B().get(); } }).start(); } } static class A{ public void get(){ //System.out.println("A from " + Thread.currentThread().getName() + " get data " +threadData.get(Thread.currentThread()));//线程内的数据 //System.out.println("A from " + Thread.currentThread().getName() + " get data " + threadLocal.get());//ThreadLocal方式实现线程内的数据共享 //System.out.println("A from " + Thread.currentThread().getName() + " get data " + threadLocalPerson.get());//ThreadLocal方式实现线程内的数据共享 System.out.println("A from " + Thread.currentThread().getName() + " get data " + Person.getThreadLocalInstance()); } } static class B{ public void get(){ //System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadData.get(Thread.currentThread()));//线程内的数据 //System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadLocal.get());//线程内的数据 //System.out.println("B from " + Thread.currentThread().getName() + " get data " + threadLocalPerson.get());//ThreadLocal方式实现线程内的数据共享 System.out.println("A from " + Thread.currentThread().getName() + " get data " + Person.getThreadLocalInstance()); } } } class Person{ private Person(){} //与线程有关的单例 public static /*synchronized*/ Person getThreadLocalInstance(){ Person person = threadLocal.get();//与本线程有关的Person对象 if(person == null){ person = new Person(); threadLocal.set(person); //与本线程有关的Person对象 } return person;//与本线程有关的Person对象 } //private static Person person = null; //new Person(); private static ThreadLocal<Person> threadLocal = new ThreadLocal<Person>(); 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; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } }
参考链接
https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html
相关文章推荐
- ThreadLocal实现线程范围的共享变量
- ThreadLocal实现线程范围的共享变量
- 多线程编程入门(7):线程范围内的共享变量
- Java多线程编程之ThreadLocal线程范围内的共享变量
- 六:java中线程范围内共享变量--ThreadLocal
- ThreadLocal实现线程范围的共享变量
- ThreadLocal实现线程范围的共享变量
- ThreadLocal实现线程范围的共享变量-笔记整理4
- ThreadLocal 线程范围内的共享变量
- Java多线程——线程范围内共享变量和ThreadLocal
- Thread学习(八) ThreadLocal实现线程范围内的共享变量
- (三) Java多线程详解之线程范围内共享变量及ThreadLocal类使用
- Java线程总结(四):ThreadLocal实现线程范围的共享变量
- ThreadLocal实现线程范围的共享变量 代码示例
- ThreadLocal实现线程范围内共享变量和对象
- ThreadLocal实现线程范围的共享变量
- 【线程高新】——【ThreadLocal实现线程范围的共享变量】
- 黑马程序员--javaSE--ThreadLocal实现线程范围的共享变量
- 线程范围内共享变量使用ThreadLocal类详细方法
- ThreadLocal实现线程范围的共享变量