Jstack is simple to use to locate endless loops, thread blocking, deadlocks, and other issues. jstack threads

Source: Internet
Author: User

Jstack is simple to use to locate endless loops, thread blocking, deadlocks, and other issues. jstack threads

Address: http://blog.csdn.net/wanglha/article/details/51133819


When we run a java program and find that the program is not moving, but we don't know where the problem is, we can use the jstack tool that comes with JDK to locate it;

Let's just look at the example on the window platform;

Endless loop

The Program for writing an endless loop is as follows:

package concurrency;public class Test {    public static void main(String[] args) throws InterruptedException {        while (true) {        }    }}

Run the above program first, and the program enters an endless loop;

Open cmd and enter the jps command. jps can directly display the java Process pid, Which is 7588 as follows:

Enter tasklistand find the PID of javaw.exe. The value is 7588 as follows:

Enter the jstack 7588 command and find the thread related to our own code. The main thread is in the runnable State. In the eighth line of the main method, that is, the position of our endless loop:

Object. wait ()

Write a small program and call wait to wait for one of the threads as follows:

Package concurrency; import java. util. concurrent. executorService; import java. util. concurrent. executors; class TestTask implements Runnable {@ Override public void run () {synchronized (this) {try {// wait for wait ();} catch (InterruptedException e) {e. printStackTrace () ;}}} public class Test {public static void main (String [] args) throws InterruptedException {ExecutorService ex = Executors. newFixedThreadPool (1); ex.exe cute (new TestTask ());}}

In the same example, we first find the PID of javaw.exe, and then use jstack to analyze the PID. Soon we find a thread in the WAITING state, in the Test. the 13 rows in the java file are exactly where we call the wait method. This indicates that the thread has not waited for y yet, as shown below:

Deadlock

Write a simple deadlock example as follows:

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) {// It is recommended that the thread interceptor switch to another thread Run 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); // starts 10 threads for (int I = 0; I <10; I ++) {int order = I % 2 = 0? 1: 0; ex.exe cute (new TestTask (order, obj1, obj2 ));}}}

In the same example, we first find the javaw.exe PID, and then use jstack to analyze the PID. Soon jstack helped us find the deadlock location, as shown below:

Wait for 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。");    }}

In the same example, we first find the javaw.exe PID, and then use jstack to analyze the PID. Soon jstack helped us find the location, with 12 lines of the Test. java file, as shown below:

 

Others

For example, if sleep is called to make the thread sleep, and suspend () is used to suspend the thread, It is similar;

 


View comments

Related Article

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.