public class Test extends Thread {
char name;
Test(char a) { name = a; }
public synchronized void run() {
for (int i=0; i<3; i++) {
System.out.print(name);
}
}
public static void main(String[] args) {
new Test('A').start();
new Test('B').start();
new Test('C').start();
}
}
synchronized的作用不是讓同時間只能一個thread執行method嗎?
因此某個thread進入run()執行後
for迴圈三次應該要跑完才會被其他thread搶到執行權嗎?
想請問為何run()加了synchronized
還是會得到 AACCCBBBA 這樣的輸出
先謝謝各位的解答了~