When we run the Java program, found that the program does not move, but do not know where the problem, you can use the JDK's own jstack tool to locate;
Nonsense not to say, directly on the example bar, on the window platform;
Dead loop
The procedure for writing a dead loop is as follows:
Package concurrency; Public class Test { publicstaticvoidthrows interruptedexception { while (true) { }} }
First run the above program, the program into the dead loop;
Open cmd, enter the JPS command, JPS very simple can directly display the Java process PID, the following is 7588:
or enter Tasklist, find the PID of Javaw.exe, the following is 7588:
Enter the Jstack 7588 command to find the thread that is related to our own code, which is the main thread, in the runnable state, in the eighth line of the main method, where we are looping:
Object.wait () situation
Write a small program that calls wait to make one of the thread waits, as follows:
Packageconcurrency;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;classTesttaskImplementsRunnable {@Override Public voidrun () {synchronized( This) { Try { //waiting to be awakenedwait (); } Catch(interruptedexception e) {e.printstacktrace (); } } }} Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {Executorservice ex= Executors.newfixedthreadpool (1); Ex.execute (NewTesttask ()); }}
Also we first find Javaw.exe pid, and then use Jstack to analyze the PID, we soon found a thread in waiting state, in the Test.java file 13 line, is where we call the wait method, that the thread has not yet waited for Notify, as follows :
Dead lock
Write a simple example of a deadlock, as follows:
Packageconcurrency;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;classTesttaskImplementsRunnable {PrivateObject obj1; PrivateObject Obj2; Private intorder; PublicTesttask (intorder, Object obj1, Object obj2) { This. Order =order; This. obj1 =obj1; This. Obj2 =Obj2; } Public voidTest1 ()throwsinterruptedexception {synchronized(obj1) {//recommend that the thread picker switch to another thread to runThread.yield (); synchronized(obj2) {System.out.println ("Test ... "); } } } Public voidTest2 ()throwsinterruptedexception {synchronized(obj2) {Thread.yield (); synchronized(obj1) {System.out.println ("Test ... "); } }} @Override Public voidrun () { while(true) { Try { if( This. order = = 1){ This. Test1 (); }Else{ This. Test2 (); } } Catch(interruptedexception e) {e.printstacktrace (); } } }} Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {Object obj1=NewObject (); Object Obj2=NewObject (); Executorservice ex= Executors.newfixedthreadpool (10); //up to 10 threads for(inti = 0; I < 10; i++) { intOrder = i%2==0? 1:0; Ex.execute (NewTesttask (Order, obj1, obj2)); } }}
We also first find Javaw.exe PID, and then use Jstack to analyze the PID, and soon Jstack help us find the location of the deadlock , as shown below:
Wait IO
Write a simple example of waiting for user input:
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. "); }}
Also we first find Javaw.exe pid, and then use jstack analysis of the PID, soon Jstack help us find the location, Test.java file 12 lines , as follows:
Other
Like calling sleep to put a thread into sleep,suspend () pauses the thread and so on is not an example, it is similar;
"JAVA Tools" jstack simple to use, locating dead loops, thread blocking, deadlock, and other issues