[問題] 關於synchronized
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 這樣的輸出
先謝謝各位的解答了~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.218.53.221
※ 文章網址: https://www.ptt.cc/bbs/java/M.1448271196.A.B38.html
→
11/23 17:37, , 1F
11/23 17:37, 1F
→
11/23 18:50, , 2F
11/23 18:50, 2F
→
11/23 20:42, , 3F
11/23 20:42, 3F
→
11/23 20:43, , 4F
11/23 20:43, 4F
→
11/23 21:06, , 5F
11/23 21:06, 5F
→
11/23 23:32, , 6F
11/23 23:32, 6F
→
11/23 23:33, , 7F
11/23 23:33, 7F
→
11/23 23:35, , 8F
11/23 23:35, 8F
→
11/23 23:36, , 9F
11/23 23:36, 9F
→
11/23 23:37, , 10F
11/23 23:37, 10F
→
11/24 00:53, , 11F
11/24 00:53, 11F
→
11/24 12:52, , 12F
11/24 12:52, 12F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):