[問題] 關於JAVA synchronized的問題?

看板java作者 (BMW 428i)時間4年前 (2020/05/11 08:23), 編輯推噓0(0013)
留言13則, 3人參與, 4年前最新討論串1/1
請問各位,下面這個範例程式為什麼輸出只會有線程0執行?? ackage Thread02; public class main { public static void main(String[] args) { Mythread02 T2=new Mythread02(); Thread t1=new Thread(T2); Thread t2=new Thread(T2); Thread t3=new Thread(T2); t1.start(); t2.start(); t3.start(); } 下面是宣告的class package Thread02; public class Mythread02 implements Runnable { private int number=100; public void run() { synchronized (this) { try { Thread.sleep(100); //---------------------------------------------------------------// while (true) { if (number > 0) { System.out.println("This is Mythread02 " + Thread.currentThread().getName() + "---" + number--); } else { break; } } //---------------------------------------------------------------//] } catch (InterruptedException e) { e.printStackTrace(); } } } } ----------------------------下面是輸出結果------------------------- This is Mythread02 Thread-0---100 This is Mythread02 Thread-0---99 This is Mythread02 Thread-0---98 This is Mythread02 Thread-0---97 This is Mythread02 Thread-0---96 This is Mythread02 Thread-0---95 This is Mythread02 Thread-0---94 This is Mythread02 Thread-0---93 This is Mythread02 Thread-0---92 This is Mythread02 Thread-0---91 This is Mythread02 Thread-0---90 This is Mythread02 Thread-0---89 This is Mythread02 Thread-0---88 This is Mythread02 Thread-0---87 This is Mythread02 Thread-0---86 This is Mythread02 Thread-0---85 This is Mythread02 Thread-0---84 This is Mythread02 Thread-0---83 This is Mythread02 Thread-0---82 This is Mythread02 Thread-0---81 This is Mythread02 Thread-0---80 This is Mythread02 Thread-0---79 This is Mythread02 Thread-0---78 This is Mythread02 Thread-0---77 This is Mythread02 Thread-0---76 This is Mythread02 Thread-0---75 This is Mythread02 Thread-0---74 This is Mythread02 Thread-0---73 This is Mythread02 Thread-0---72 This is Mythread02 Thread-0---71 This is Mythread02 Thread-0---70 This is Mythread02 Thread-0---69 This is Mythread02 Thread-0---68 This is Mythread02 Thread-0---67 This is Mythread02 Thread-0---66 This is Mythread02 Thread-0---65 This is Mythread02 Thread-0---64 This is Mythread02 Thread-0---63 This is Mythread02 Thread-0---62 This is Mythread02 Thread-0---61 This is Mythread02 Thread-0---60 This is Mythread02 Thread-0---59 This is Mythread02 Thread-0---58 This is Mythread02 Thread-0---57 This is Mythread02 Thread-0---56 This is Mythread02 Thread-0---55 This is Mythread02 Thread-0---54 This is Mythread02 Thread-0---53 This is Mythread02 Thread-0---52 This is Mythread02 Thread-0---51 This is Mythread02 Thread-0---50 This is Mythread02 Thread-0---49 This is Mythread02 Thread-0---48 This is Mythread02 Thread-0---47 This is Mythread02 Thread-0---46 This is Mythread02 Thread-0---45 This is Mythread02 Thread-0---44 This is Mythread02 Thread-0---43 This is Mythread02 Thread-0---42 This is Mythread02 Thread-0---41 This is Mythread02 Thread-0---40 This is Mythread02 Thread-0---39 This is Mythread02 Thread-0---38 This is Mythread02 Thread-0---37 This is Mythread02 Thread-0---36 This is Mythread02 Thread-0---35 This is Mythread02 Thread-0---34 This is Mythread02 Thread-0---33 This is Mythread02 Thread-0---32 This is Mythread02 Thread-0---31 This is Mythread02 Thread-0---30 This is Mythread02 Thread-0---29 This is Mythread02 Thread-0---28 This is Mythread02 Thread-0---27 This is Mythread02 Thread-0---26 This is Mythread02 Thread-0---25 This is Mythread02 Thread-0---24 This is Mythread02 Thread-0---23 This is Mythread02 Thread-0---22 This is Mythread02 Thread-0---21 This is Mythread02 Thread-0---20 This is Mythread02 Thread-0---19 This is Mythread02 Thread-0---18 This is Mythread02 Thread-0---17 This is Mythread02 Thread-0---16 This is Mythread02 Thread-0---15 This is Mythread02 Thread-0---14 This is Mythread02 Thread-0---13 This is Mythread02 Thread-0---12 This is Mythread02 Thread-0---11 This is Mythread02 Thread-0---10 This is Mythread02 Thread-0---9 This is Mythread02 Thread-0---8 This is Mythread02 Thread-0---7 This is Mythread02 Thread-0---6 This is Mythread02 Thread-0---5 This is Mythread02 Thread-0---4 This is Mythread02 Thread-0---3 This is Mythread02 Thread-0---2 This is Mythread02 Thread-0---1 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.250.186.57 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/java/M.1589156638.A.C6F.html

05/11 10:02, 4年前 , 1F
在 main 主執行緒加上sleep多等幾秒看看
05/11 10:02, 1F

05/11 11:34, 4年前 , 2F
因為synchronized (this)的關係
05/11 11:34, 2F

05/11 14:09, 4年前 , 3F
在synchronized裡面放sleep並不會把lock釋放
05/11 14:09, 3F

05/11 14:18, 4年前 , 4F
另外以你的寫法是搶到的Thread就會一次跑完,如果你是要模
05/11 14:18, 4F

05/11 14:19, 4年前 , 5F
擬每個數字都重搶一次,Thread中斷應該放在迴圈裡吧
05/11 14:19, 5F

05/11 14:23, 4年前 , 6F
while (true) {
05/11 14:23, 6F

05/11 14:23, 4年前 , 7F
synchronized (this) {
05/11 14:23, 7F

05/11 14:24, 4年前 , 8F
if (...) {...} else { break; }
05/11 14:24, 8F

05/11 14:24, 4年前 , 9F
}
05/11 14:24, 9F

05/11 14:24, 4年前 , 10F
Thread.sleep(100);
05/11 14:24, 10F

05/11 14:24, 4年前 , 11F
}
05/11 14:24, 11F

05/11 14:26, 4年前 , 12F
我猜你想做的效果是這樣
05/11 14:26, 12F

05/11 14:27, 4年前 , 13F
如果一定要synchonized在外面,要用wait/notify來控制lock
05/11 14:27, 13F
文章代碼(AID): #1Uk9iUnl (java)
文章代碼(AID): #1Uk9iUnl (java)