看板 java 關於我們 聯絡資訊
各位版友好,我寫了一個簡單的程式,利用2個分別印出5行log, 結果發現thread印出的log不如預期...請問是我寫錯了嗎? Runnable runnable=new Runnable( { @Override public void run(){ for(j=0;j<5;j++) Log.i(tag,Thread.currentThread.getName()); } } for(i=0;i<2;i++) { Thread thread=new Thread(runnable); thread.setName(根據i值設定名稱); try{ thread.start(); thread.join(); }catch(InterruptedException e){} 結果一個thread只印出兩次名字,請問我有寫錯嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.192.46 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/java/M.1589162154.A.A5A.html
LZN: 有完整code嗎? 我試起來兩個thread各印5次沒錯啊05/11 11:42
ssccg: 你這程式根本不能跑,我改到能跑之後一個thread印5次沒問題05/11 14:36
https://i.imgur.com/1FHPIiM.jpg
https://i.imgur.com/ls8DHZU.jpg
※ 編輯: Dong0129 (111.249.97.154 臺灣), 05/11/2020 21:16:42
dpes10087: 沒寫錯,你直接在getName()後加上+j就會正常印出。05/12 10:51
dpes10087: 你不能顯示的主因是因為logcat的chatty機制,會把重複05/12 11:04
dpes10087: 的中間內容改以chatty : 'identical X lines'顯示。05/12 11:06
原來如此,謝謝! ※ 編輯: Dong0129 (114.137.95.33 臺灣), 05/12/2020 17:25:08