public class MyRunnable implements Runnable { @Override public void run() { Thread currentThread = Thread.currentThread(); System.out.println(currentThread.getName() + "-------------進入"); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println(currentThread.getName() + "-------------離開"); } } }
public class MyTest { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread1 = new Thread(myRunnable, "線程1"); Thread thread2 = new Thread(myRunnable, "線程2"); Thread thread3 = new Thread(myRunnable, "線程3"); thread1.start(); thread2.start(); thread3.start(); } }
下文默認線程的執行順序爲:線程1->線程2->線程3。(實際狀況不必定如此)ide
你會發現 idea 會停在斷點處,上面顯示當前線程爲「線程1」(注意,這裏可能爲「線程2」或者「線程3」,由於不肯定哪一個線程先搶到資源)。idea
而後繼續運行程序 F9,跳到下一個斷點線程
你會發現控制檯三個線程的日誌都打印出來了日誌
線程1-------------進入 線程2-------------進入 線程3-------------進入
也就是說,idea 只停留在了「線程1」的斷點上,「線程2」和「線程3」的斷點直接忽略了。code
這不是咱們想要的,咱們但願每一個線程的斷點都會停留。能夠作以下設置。blog
把 All 改成 Thread。若是點了 Make Default ,那麼後續加上的斷點都是 Thead 設置,以前加上的斷點不影響。資源
把上面的兩個斷點都改成 Thread 設置,再 Debug 運行 MyTest.main()。get
你會發現 idea 首先留在了「線程1」的斷點上,F9,繼續運行,接着會停留在 「線程2」的斷點上,F9,繼續運行,又會停留在 「線程3」的斷點上。it
和上面的效果是同樣的。io
因爲本人知識和能力有限,文中若有沒說清楚的地方,但願你們能在評論區指出,以幫助我將博文寫得更好。