java 板


LINE

請問各位,下面這個範例程式為什麼輸出只會有線程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://webptt.com/m.aspx?n=bbs/java/M.1589156638.A.C6F.html
1F:→ charlie0275: 在 main 主執行緒加上sleep多等幾秒看看 05/11 10:02
2F:→ LZN: 因為synchronized (this)的關係 05/11 11:34
3F:→ ssccg: 在synchronized裡面放sleep並不會把lock釋放 05/11 14:09
4F:→ ssccg: 另外以你的寫法是搶到的Thread就會一次跑完,如果你是要模 05/11 14:18
5F:→ ssccg: 擬每個數字都重搶一次,Thread中斷應該放在迴圈裡吧 05/11 14:19
6F:→ ssccg: while (true) { 05/11 14:23
7F:→ ssccg: synchronized (this) { 05/11 14:23
8F:→ ssccg: if (...) {...} else { break; } 05/11 14:24
9F:→ ssccg: } 05/11 14:24
10F:→ ssccg: Thread.sleep(100); 05/11 14:24
11F:→ ssccg: } 05/11 14:24
12F:→ ssccg: 我猜你想做的效果是這樣 05/11 14:26
13F:→ ssccg: 如果一定要synchonized在外面,要用wait/notify來控制lock 05/11 14:27







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP