java多線程編程中,存在很多線程安全問題,至於什么是線程安全呢,給出一個通俗易懂的概念還是蠻難的,如同《java並發編程實踐》中所說:
此處不贅述了,首先給出靜態變量、實例變量、局部變量在多線程環境下的線程安全問題結論,然后用示例驗證,請大家擦亮眼睛,有錯必究,否則誤人子弟!
靜態變量:線程非安全。
靜態變量即類變量,位於方法區,為所有對象共享,共享一份內存,一旦靜態變量被修改,其他對象均對修改可見,故線程非安全。
實例變量:單例模式(只有一個對象實例存在)線程非安全,非單例線程安全。
實例變量為對象實例私有,在虛擬機的堆中分配,若在系統中只存在一個此對象的實例,在多線程環境下,“猶如”靜態變量那樣,被某個線程修改后,其他線程對修改均可見,故線程非安全;如果每個線程執行都是在不同的對象中,那對象與對象之間的實例變量的修改將互不影響,故線程安全。
局部變量:線程安全。
每個線程執行時將會把局部變量放在各自棧幀的工作內存中,線程間不共享,故不存在線程安全問題。
靜態變量線程安全問題模擬:
----------------------------------------------------------------------------------
- /**
- * 線程安全問題模擬執行
- * ------------------------------
- * 線程1 | 線程2
- * ------------------------------
- * static_i = 4; | 等待
- * static_i = 10; | 等待
- * 等待 | static_i = 4;
- * static_i * 2; | 等待
- * -----------------------------
- * */
- public class Test implements Runnable
- {
- private static int static_i;//靜態變量
- public void run()
- {
- static_i = 4;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取static_i 的值:" + static_i);
- static_i = 10;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取static_i*3的值:" + static_i * 2);
- }
- public static void main(String[] args)
- {
- Test t = new Test();
- //啟動盡量多的線程才能很容易的模擬問題
- for (int i = 0; i < 3000; i++)
- {
- //t可以換成new Test(),保證每個線程都在不同的對象中執行,結果一樣
- new Thread(t, "線程" + i).start();
- }
- }
- }
根據代碼注釋中模擬的情況,當線程1執行了static_i = 4; static_i = 10; 后,線程2獲得執行權,static_i = 4; 然后當線程1獲得執行權執行static_i * 2; 必然輸出結果4*2=8,按照這個模擬,我們可能會在控制台看到輸出為8的結果。
[線程22]獲取static_i*2的值:20
[線程28]獲取static_i 的值:4
[線程23]獲取static_i*2的值:8
[線程29]獲取static_i 的值:4
[線程30]獲取static_i 的值:4
[線程31]獲取static_i 的值:4
[線程24]獲取static_i*2的值:20
看紅色標注的部分,確實出現了我們的預想,同樣也證明了我們的結論。
實例變量線程安全問題模擬:
----------------------------------------------------------------------------------
- public class Test implements Runnable
- {
- private int instance_i;//實例變量
- public void run()
- {
- instance_i = 4;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取instance_i 的值:" + instance_i);
- instance_i = 10;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取instance_i*3的值:" + instance_i * 2);
- }
- public static void main(String[] args)
- {
- Test t = new Test();
- //啟動盡量多的線程才能很容易的模擬問題
- for (int i = 0; i < 3000; i++)
- {
- //每個線程對在對象t中運行,模擬單例情況
- new Thread(t, "線程" + i).start();
- }
- }
- }
按照本文開頭的分析,猶如靜態變量那樣,每個線程都在修改同一個對象的實例變量,肯定會出現線程安全問題。
寫道
[線程66]獲取instance_i 的值:10
[線程33]獲取instance_i*2的值:20
[線程67]獲取instance_i 的值:4
[線程34]獲取instance_i*2的值:8
[線程35]獲取instance_i*2的值:20
[線程68]獲取instance_i 的值:4
看紅色字體,可知單例情況下,實例變量線程非安全。
將new Thread(t, "線程" + i).start();改成new Thread(new Test(), "線程" + i).start();模擬非單例情況,會發現不存在線程安全問題。
局部變量線程安全問題模擬:
----------------------------------------------------------------------------------
- public class Test implements Runnable
- {
- public void run()
- {
- int local_i = 4;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取local_i 的值:" + local_i);
- local_i = 10;
- System.out.println("[" + Thread.currentThread().getName()
- + "]獲取local_i*2的值:" + local_i * 2);
- }
- public static void main(String[] args)
- {
- Test t = new Test();
- //啟動盡量多的線程才能很容易的模擬問題
- for (int i = 0; i < 3000; i++)
- {
- //每個線程對在對象t中運行,模擬單例情況
- new Thread(t, "線程" + i).start();
- }
- }
- }
控制台沒有出現異常數據。
---------------------------------------------------------------
以上只是通過簡單的實例來展示靜態變量、實例變量、局部變量等的線程安全問題,
並未進行底層的分析,下一篇將對線程問題的底層進行剖析。
靜態方法是線程安全的
先看一個類
public class Test{
public static String hello(String str){
String tmp="";
tmp = tmp+str;
return tmp;
}
}
hello方法會不會有多線程安全問題呢?沒有!!
靜態方法如果沒有使用靜態變量,則沒有線程安全問題。
為什么呢?因為靜態方法內聲明的變量,每個線程調用時,都會新創建一份,而不會共用一個存儲單元。比如這里的tmp,每個線程都會創建自己的一份,因此不會有線程安全問題
注意,靜態變量,由於是在類加載時占用一個存儲區,每個線程都是共用這個存儲區的,所以如果在靜態方法里使用了靜態變量,這就會有線程安全問題!
總結:只要方法內含有靜態變量,就是非線程安全的
來自:http://blog.csdn.net/aaa1117a8w5s6d/article/details/8295527