以前用ThreadLocal用的比較少,今天在看以前的代碼的時候,發現有個類裏面的方法裏面使用了公共的靜態變量,而且有很多類都引用了這個方法。由於這個變量不是final的,是動態賦值的,所以在多線程的情況下可能會引起問題。因爲要改造這個方法的這個變量問題,所以我想到了ThreadLocal,並對ThreadLocal進行了一次實驗,通過實驗,對ThreadLocal有了一次比較深刻的認識。在此把代碼貼出來,跟各位共同學習一下。
public class Test { public static ThreadLocal<String> threadLocal = new ThreadLocal<String>(); private static final long waitTime = 3000L; public static void main(String[] args) throws Exception { Thread.sleep(60000); Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----111111111"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user = new User("張三"); user.say(); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----22222222"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user = new User("李四"); user.say(); } }); Thread thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----3333333333"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } User user1 = new User("王五"); user1.say(); User user2 = new User("趙六"); user2.say(); } }); thread1.start(); thread2.start(); thread3.start(); Thread thread4 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); threadLocal.set(Thread.currentThread().getName()+"----4444444444"); try { Thread.sleep(waitTime); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new Runnable() { @Override public void run() { User user1 = new User("宋八1"); user1.say(); threadLocal.set(Thread.currentThread().getName()+"----5555555555"); User user2 = new User("宋八2"); user2.say(); } }).start(); new Thread(new Runnable() { @Override public void run() { User user1 = new User("錢九1"); user1.say(); threadLocal.set(Thread.currentThread().getName()+"----666666666666666"); User user2 = new User("錢九2"); user2.say(); } }).start(); } }); thread4.start(); Thread.sleep(60000); }
}
public class User { private String name; public User(){ super(); } public User(String name){ this.name = name; } public void say(){ System.out.println(name + " say:" + Test.threadLocal.get()); }
}
結果輸出如下:
Thread-2
Thread-3
Thread-4
Thread-5
張三 say:Thread-2----111111111
李四 say:Thread-3----22222222
王五 say:Thread-4----3333333333
趙六 say:Thread-4----3333333333
錢九1 say:null
錢九2 say:Thread-7----666666666666666
宋八1 say:null
宋八2 say:Thread-6----5555555555
做一下說明:
我創建了四個主線程,每個線程內部對ThreadLocal變量進行賦值,並在主線程內創建User對象,User對象會輸出ThreadLocal的值。通過結果可以看出:
1、各個線程內部的ThreadLocal是不相互影響的,並且主線程與子線程是不相互影響的。
2、同一個線程內容,不同對象獲取的同一個ThreadLocal對象是一樣的。
3、子線程是無法獲取到主線程的ThreadLocal值的
通過以上試驗,我還進行一下內存的監測,發現一個線程調用結束之後,會進行對象的回收。如圖:
通過
以上的試驗,希望對大家理解和使用ThreadLocal能有所幫助。