帶同步的簡單多線程測試。我想如果它是「同步的」,其他線程會等待。我錯過了什麼?使用同步的Java多線程不是線程安全的
public class MultithreadingCounter implements Runnable {
static int count = 0;
public static void main(String[] args) {
int numThreads = 4;
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++)
threads[i] = new Thread(new MultithreadingCounter(), i + "");
for (int i = 0; i < numThreads; i++)
threads[i].start();
for (int i = 0; i < numThreads; i++)
try {
threads[i].join();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
increment();
}
public synchronized void increment(){
System.out.print(Thread.currentThread().getName() + ": " + count + "\t");
count++; // if I put this first or increment it directly in the print line, it works fine.
}
}
我認爲這將顯示類似:
0: 1 2: 0 1: 2 3: 3
但其實際輸出:
0: 0 2: 0 1: 0 3: 3
和其他變化是這樣的。它應該顯示每個增量(即0,1,2,3)不按順序...
啊。我每天都會學到新的東西。謝謝。 – user1877411
會使它成爲'static'(與'synchronized'結合)充分地關閉它,還是應該使變量volatile? –
(正確)同步就足夠了。它已確保增量與後續讀取之間具有發生之前的關係,這是「易失性」會增加的所有內容。 –