您好,登錄后才能下訂單哦!
java 中ThreadLocal實例分析
從概念上理解,threadlocal使變量在多個線程中相互隔離實現線程安全,threadlocal包裝的變量最終都專屬于對應的每個線程,線程之間相互獨立,用一個具體實現來說明:
public interface Consumer { int consume(); } public class ComsumeThread implements Runnable { private Consumer consumer; public ComsumeThread(Consumer consumer) { this.consumer = consumer; } @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+" After Consume left:"+consumer.consume()); } } } public class ConsumeClientA implements Consumer { private static int leftNum = 30; @Override public int consume() { int orgLeftNum = leftNum; Random random = new Random(System.currentTimeMillis()); try { Thread.sleep(random.nextInt(3)); } catch (InterruptedException e) { e.printStackTrace(); } orgLeftNum = orgLeftNum -1; leftNum = orgLeftNum; return leftNum; } public static void main(String[] args){ Consumer consumer = new ConsumeClientA(); Thread thread1 = new Thread(new ComsumeThread(consumer)); Thread thread2 = new Thread(new ComsumeThread(consumer)); Thread thread3 = new Thread(new ComsumeThread(consumer)); thread1.start(); thread2.start(); thread3.start(); } }
ConsumeClientA是在沒有做任何線程安全處理,結果如下:
Thread-2 After Consume left:29 Thread-1 After Consume left:29 Thread-3 After Consume left:29 Thread-2 After Consume left:28 Thread-1 After Consume left:28 Thread-3 After Consume left:28 Thread-2 After Consume left:27 Thread-1 After Consume left:27 Thread-2 After Consume left:26 Thread-3 After Consume left:27 Thread-1 After Consume left:25 Thread-2 After Consume left:25 Thread-3 After Consume left:25 Thread-1 After Consume left:24 Thread-2 After Consume left:24 Thread-3 After Consume left:24 Thread-1 After Consume left:23 Thread-2 After Consume left:23 Thread-3 After Consume left:23 Thread-1 After Consume left:22 Thread-2 After Consume left:22 Thread-3 After Consume left:22 Thread-1 After Consume left:21 Thread-2 After Consume left:21 Thread-3 After Consume left:21 Thread-1 After Consume left:20 Thread-2 After Consume left:20 Thread-3 After Consume left:20 Thread-1 After Consume left:19 Thread-3 After Consume left:18
增加threadlocal處理,每個線程相互獨立,實現如下:
public class ConsumeClientB implements Consumer { private ThreadLocal<Integer> leftNumThreadLocal = new ThreadLocal<Integer>(){ @Override protected Integer initialValue() { return 30; } }; @Override public int consume() { int orgLeftNum = leftNumThreadLocal.get(); Random random = new Random(System.currentTimeMillis()); try { Thread.sleep(random.nextInt(3)); } catch (InterruptedException e) { e.printStackTrace(); } orgLeftNum = orgLeftNum -1; leftNumThreadLocal.set(orgLeftNum); return leftNumThreadLocal.get(); } public static void main(String[] args){ Consumer consumer = new ConsumeClientB(); Thread thread1 = new Thread(new ComsumeThread(consumer)); Thread thread2 = new Thread(new ComsumeThread(consumer)); Thread thread3 = new Thread(new ComsumeThread(consumer)); thread1.start(); thread2.start(); thread3.start(); } }
運行的結果如下:
Thread-1 After Consume left:29 Thread-3 After Consume left:29 Thread-2 After Consume left:29 Thread-1 After Consume left:28 Thread-3 After Consume left:28 Thread-2 After Consume left:28 Thread-1 After Consume left:27 Thread-3 After Consume left:27 Thread-2 After Consume left:27 Thread-1 After Consume left:26 Thread-3 After Consume left:26 Thread-2 After Consume left:26 Thread-1 After Consume left:25 Thread-3 After Consume left:25 Thread-2 After Consume left:25 Thread-1 After Consume left:24 Thread-3 After Consume left:24 Thread-2 After Consume left:24 Thread-1 After Consume left:23 Thread-3 After Consume left:23 Thread-2 After Consume left:23 Thread-1 After Consume left:22 Thread-3 After Consume left:22 Thread-2 After Consume left:22 Thread-1 After Consume left:21 Thread-3 After Consume left:21 Thread-2 After Consume left:21 Thread-1 After Consume left:20 Thread-3 After Consume left:20 Thread-2 After Consume left:20
每個線程擁有自己的獨立變量,相互隔離實現線程安全。
那ThreadLocal是怎樣實現這種線程隔離的線程安全的呢?
從ThreadLocal源碼可以看到,真正實現線程隔離,與線程掛鉤的,其實是ThreadLocal.ThreadLocalMap這個實現類,最明顯的體現就在于Thread類源碼的這樣一個變量申明說明了ThreadLocal.ThreadLocalMap與Thread的關系:
ThreadLocal.ThreadLocalMap threadLocals, inheritableThreadLocals;
Thread類是包含threadLocals對象的,ThreadLocal的具體實現就是根據提供的get,set等接口,對當前thread的threadLocals變量進行相關操作的,如get操作代碼如下:
public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) return (T)e.value; } return setInitialValue(); } ThreadLocal.ThreadLocalMap getMap(Thread t) { return t.threadLocals; }
可以看到,getMap()方法就是從當前thread獲取對應的threadLocals變量,然后從這個ThreadLocal.ThreadLocalMap類型的threadLocals變量中獲取對應線程中該ThreadLocal對象對應的變量值。
set方法的操作也是一樣:
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocal.ThreadLocalMap map = getMap(t); if(map != null) { map.set(this, value); } else { this.createMap(t, value); } } void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); }
static class Entry extends WeakReference<ThreadLocal> { Object value; Entry(ThreadLocal var1, Object var2) { super(var1); this.value = var2; } }
ThreadLocalMap中存的是內部類Entry的數組,Entry是繼承WeakReference實現,WeakReference的好處是保存對象引用,而又不干擾該對象被GC回收,線程執行完回收threadLocals變量時不會受到Entry封裝的變量的干擾。
而且ThreadLocalMap中的key是ThreadLocal,所以一個ThreadLocal對象只能在一個Thread對象中保存一個ThreadLocal的value。
綜上,很多人說ThreadLocal的實現是ThreadLocalMap中存Thread對象為key,變量為value的map結構,其實是錯誤的。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。