[Java] threadLocal 实现线程范围内变量共享 →→→→→进入此内容的聊天室

来自 , 2019-05-19, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/cd0dce8f
  1. package cn.dandan.test;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Random;
  6.  
  7. public class ThreadLocalTest {
  8.  
  9.         private static ThreadLocal<Integer> x = new ThreadLocal<Integer>();
  10.         private static ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();
  11.         public static void main(String[] args) {
  12.                 for(int i=0;i<2;i++){
  13.                         new Thread(new Runnable(){
  14.                                 @Override
  15.                                 public void run() {
  16.                                         int data = new Random().nextInt();
  17.                                         System.out.println(Thread.currentThread().getName()
  18.                                                         + " has put data :" + data);
  19.                                         x.set(data);
  20. /*                                      MyThreadScopeData myData = new MyThreadScopeData();
  21.                                         myData.setName("name" + data);
  22.                                         myData.setAge(data);
  23.                                         myThreadScopeData.set(myData);*/
  24.                                         MyThreadScopeData.getThreadInstance().setName("name" + data);
  25.                                         MyThreadScopeData.getThreadInstance().setAge(data);
  26.                                         new A().get();
  27.                                         new B().get();
  28.                                 }
  29.                         }).start();
  30.                 }
  31.         }
  32.        
  33.         static class A{
  34.                 public void get(){
  35.                         int data = x.get();
  36.                         System.out.println("A from " + Thread.currentThread().getName()
  37.                                         + " get data :" + data);
  38. /*                      MyThreadScopeData myData = myThreadScopeData.get();;
  39.                         System.out.println("A from " + Thread.currentThread().getName()
  40.                                         + " getMyData: " + myData.getName() + "," +
  41.                                         myData.getAge());*/
  42.                         MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
  43.                         System.out.println("A from " + Thread.currentThread().getName()
  44.                                         + " getMyData: " + myData.getName() + "," +
  45.                                         myData.getAge());
  46.                 }
  47.         }
  48.        
  49.         static class B{
  50.                 public void get(){
  51.                         int data = x.get();                    
  52.                         System.out.println("B from " + Thread.currentThread().getName()
  53.                                         + " get data :" + data);
  54.                         MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();
  55.                         System.out.println("B from " + Thread.currentThread().getName()
  56.                                         + " getMyData: " + myData.getName() + "," +
  57.                                         myData.getAge());                      
  58.                 }              
  59.         }
  60. }
  61.  
  62. class MyThreadScopeData{
  63.         private MyThreadScopeData(){}
  64.         public static /*synchronized*/ MyThreadScopeData getThreadInstance(){
  65.                 MyThreadScopeData instance = map.get();
  66.                 if(instance == null){
  67.                         instance = new MyThreadScopeData();
  68.                         map.set(instance);
  69.                 }
  70.                 return instance;
  71.         }
  72.         //private static MyThreadScopeData instance = null;//new MyThreadScopeData();
  73.         private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();
  74.        
  75.         private String name;
  76.         private int age;
  77.         public String getName() {
  78.                 return name;
  79.         }
  80.         public void setName(String name) {
  81.                 this.name = name;
  82.         }
  83.         public int getAge() {
  84.                 return age;
  85.         }
  86.         public void setAge(int age) {
  87.                 this.age = age;
  88.         }
  89. }
  90.  

回复 "threadLocal 实现线程范围内变量共享"

这儿你可以回复上面这条便签

captcha