java Command-jstack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.