標籤:張孝祥 str 自動 沒有 rand span 線上 其他屬性 nbsp
今天我們學習的是如何線上程自己的範圍內達到變數資料的共用,而各個線程之間又是互相獨立開來,各自維護的,即我們說的ThreadLocal的作用。
一、概念
可以將每個線程用到的資料與對應的線程號存放到一個map集合中,使用資料時從這個集合中根據線程號擷取對應線程的資料,就可以實現線程範圍內共用相同的變數。
二、代碼
Runnable中的run()方法裡面執行Thread.currentThread()都會對應當前Runnable對應的線程,因此A、B中對應的Thread.currentThread()對應所在的Runnable對應的線程
public class ThreadScopeShareData { private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>(); 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); new A().get(); new B().get(); } }).start(); } } static class A{ public void get(){ int data = threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " get data :" + data); } } static class B{ public void get(){ int data = threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " get data :" + data); } }}
三、ThreadLocal
JDK1.5提供了ThreadLocal類來方便實現線程範圍內的資料共用,它的作用就相當於前面中的Map(內部並不是Map),也就是讓每個線程擁有自己的值
一個ThreadLocal對象只能記錄一個線程內部的一個共用變數,需要記錄多個共用資料,可以建立多個ThreadLocal對象,或者將這些資料進行封裝,將封裝後的資料對象存入ThreadLocal對象中。
線程結束後也可以自動釋放相關的ThreadLocal變數,也可以調用ThreadLocal.remove()方法用來更快釋放記憶體。
代碼:
public class ThreadLocalTest { private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(); 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(100); System.out.println(Thread.currentThread().getName()+" has put data: "+data); //往local裡面設定值 threadLocal.set(data); new A().get(); new B().get(); } }).start(); } } static class A{ public void get(){ int data =threadLocal.get(); System.out.println("A from "+Thread.currentThread().getName()+" has get data: "+data); } } static class B{ public void get(){ int data =threadLocal.get(); System.out.println("B from "+Thread.currentThread().getName()+" has get data: "+data); } } }
假設需要儲存不止一個值,可以把其他屬性的值打包成一個類,然後將該類設定成ThreadLocal的值。
下面代碼中,在類MyThreadLocalScopeDate裡面定義了一個靜態變數Map,用來儲存所有線程建立的MyThreadLocalScopeDate,並使用單例使得不管多少線程都只建立一個MyThreadLocalScopeDate對象。
public class ThreadLocalTest { private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(); 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(100); System.out.println(Thread.currentThread().getName()+" has put data: "+data); //往local裡面設定值 threadLocal.set(data); //擷取自己線程的MyThreadLocalScopeDate執行個體對象 MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance(); myData.setName("name"+data); myData.setAge(data); new A().get(); new B().get(); } }).start(); } } static class A{ public void get(){ int data =threadLocal.get(); System.out.println("A from "+Thread.currentThread().getName()+" has get data: "+data); MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance(); System.out.println("A from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge()); } } static class B{ public void get(){ int data =threadLocal.get(); System.out.println("B from "+Thread.currentThread().getName()+" has get data: "+data); MyThreadLocalScopeDate myData = MyThreadLocalScopeDate.getThreadInstance(); System.out.println("B from "+Thread.currentThread().getName()+" has get MyThreadLocalScopeDate name: "+myData.getName()+" , age: "+myData.getAge()); } } } class MyThreadLocalScopeDate{//單例模式 private MyThreadLocalScopeDate(){};//構造方法私人化 private static ThreadLocal<MyThreadLocalScopeDate> map = new ThreadLocal<MyThreadLocalScopeDate>();//封裝MyThreadLocalScopeDate是線程實現範圍內共用 //思考AB兩個線程過來的情況 自己分析 AB都需要的自己的對象 沒有關係 所以不需要同步 如果有關係就需要同步了 public static /*synchronized*/MyThreadLocalScopeDate getThreadInstance(){ MyThreadLocalScopeDate instance =map.get(); if(instance==null){ instance = new MyThreadLocalScopeDate(); map.set(instance); } return instance; } 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; } }
參考資料:
《多線程視頻》張孝祥
JAVA多線程提高三:線程範圍內共用變數&ThreadLocal