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