Jstack is used to produce a thread snapshot of the current moment of the Java virtual machine. Thread snapshots are the methods that each thread in the current Java Virtual machine is executing
Collection of stacks, the primary purpose of generating a thread snapshot is to locate the cause of a thread's long pause, such as inter-thread deadlock, dead loop, request
External resources cause long waits and so on. The call stack for each thread can be viewed by the Jstack command when a thread pauses
Thread state
New is not started
Runnable within a virtual machine
Blocked blocking and waiting for monitor lock
Waiting indefinitely waits for another thread to perform a specific action
Timed_waiting to wait for a specific action on another thread
Terminated has exited
Jstack using a simple example (deadlock scenario simulation)
1 Public classDeadLock {2 3 Public Static voidMain (string[] args) {4 //gets the current thread PID5 System.out.println (Managementfactory.getruntimemxbean (). GetName ());6Thread T1 =NewThread (NewDeadlockclass (true));7Thread t2 =NewThread (NewDeadlockclass (false));8 T1.start ();9 T2.start ();Ten } One } A - - classDeadlockclassImplementsRunnable { the Public BooleanFlag; - - PublicDeadlockclass (Booleanflag) { - This. Flag =Flag; + } - + @Override A Public voidrun () { at if(flag) { - while(true) { - synchronized(suoobject.obj_1) { -System.out.println ("Obj_1" +Thread.CurrentThread (). GetName ()); - synchronized(suoobject.obj_2) { -System.out.println ("obj_2" +Thread.CurrentThread (). GetName ()); in } - } to } +}Else { - while(true) { the synchronized(suoobject.obj_2) { *System.out.println ("obj_2" +Thread.CurrentThread (). GetName ()); $ synchronized(suoobject.obj_1) {Panax NotoginsengSystem.out.println ("Obj_1" +Thread.CurrentThread (). GetName ()); - } the } + } A } the } + } - $ classSuoobject { $ StaticObject obj_1 =NewObject (); - StaticObject obj_2 =NewObject (); -}
According to the print out of the thread PID, using the Jstack PID command, you can see the following as shown in the results, deadlock information at a glance!!!
java Command-jstack