當我們運行java程序時,發現程序不動,但又不知道是哪裡出問題時,可以使用JDK自帶的jstack工具去定位;
廢話不說,直接上例子吧,在window平台上的;
寫個死循環的程序如下:
package concurrency; public class Test { public static void main(String[] args) throws InterruptedException { while (true) { } } }
先運行以上程序,程序進入死循環;
打開cmd,輸入jps命令,jps很簡單可以直接顯示java進程的pid,如下為7588:
或者輸入tasklist,找到javaw.exe的PID,如下為7588:
輸入jstack 7588命令,找到跟我們自己代碼相關的線程,如下為main線程,處於runnable狀態,在main方法的第八行,也就是我們死循環的位置:
寫個小程序,調用wait使其中一線程等待,如下:
package concurrency; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class TestTask implements Runnable { @Override public void run() { synchronized (this) { try { //等待被喚醒 wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main(String[] args) throws InterruptedException { ExecutorService ex = Executors.newFixedThreadPool(1); ex.execute(new TestTask()); } }
同樣我們先找到javaw.exe的PID,再利用jstack分析該PID,很快我們就找到了一個線程處於WAITING狀態,在Test.java文件13行處,正是我們調用wait方法的地方,說明該線程目前還沒等到notify,如下:
寫個簡單的死鎖例子,如下:
package concurrency; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class TestTask implements Runnable { private Object obj1; private Object obj2; private int order; public TestTask(int order, Object obj1, Object obj2) { this.order = order; this.obj1 = obj1; this.obj2 = obj2; } public void test1() throws InterruptedException { synchronized (obj1) { //建議線程調取器切換到其它線程運行 Thread.yield(); synchronized (obj2) { System.out.println("test。。。"); } } } public void test2() throws InterruptedException { synchronized (obj2) { Thread.yield(); synchronized (obj1) { System.out.println("test。。。"); } } } @Override public void run() { while (true) { try { if(this.order == 1){ this.test1(); }else{ this.test2(); } } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main(String[] args) throws InterruptedException { Object obj1 = new Object(); Object obj2 = new Object(); ExecutorService ex = Executors.newFixedThreadPool(10); // 起10個線程 for (int i = 0; i < 10; i++) { int order = i%2==0 ? 1 : 0; ex.execute(new TestTask(order, obj1, obj2)); } } }
同樣我們先找到javaw.exe的PID,再利用jstack分析該PID,很快jstack就幫我們找到了死鎖的位置,如下所示:
寫個簡單的等待用戶輸入例子:
package concurrency; import java.io.IOException; import java.io.InputStream; public class Test { public static void main(String[] args) throws InterruptedException, IOException { InputStream is = System.in; int i = is.read(); System.out.println("exit。"); } }
同樣我們先找到javaw.exe的PID,再利用jstack分析該PID,很快jstack就幫我們找到了位置,Test.java文件12行,如下所示:
像調用sleep使線程進入睡眠,suspend()暫停線程等就不舉例了,都是類似的;