当前位置:网站首页>Jstack of JVM command: print thread snapshots in JVM

Jstack of JVM command: print thread snapshots in JVM

2022-07-07 06:08:00 Zhang Junjie 1994

Study Silicon Valley Song Hongkang JVM From entry to mastery Learning notes of

summary

jstack(JVM Stack Trace) It is used to generate a thread snapshot of the current time of the specified process of the virtual machine ( Virtual machine stack trace ), Thread snapshot is the collection of method stacks executed by each thread of the specified process in the current virtual machine .
The function of generating snapshots of threads : There may be a long pause during the execution of multithreads , When threads compete for resources, some threads need to wait for synchronization monitor or deadlock , Or dead circulation , And so on, the above conditions will lead to abnormal threads , There will be a long pause , You want to know which piece of code caused the thread to pause , This is the time jstack Instructions

image.png

image.png

grammar

Basic grammar
option Parameters :-F
When a normal output request is not responded to , Force output thread stack
option Parameters :-l
Besides the stack , Show additional information about locks
option Parameters :-m
If you call a local method , Can be displayed C/C++ The stack
option Parameters :-h
Help operate

Basic grammar description :
image.png
image.png
Examples are as follows :
1.image.png
2. Add -l Parameters :image.png
summary :
If the program has a wait problem , You can use this instruction to check the problem , The result will also remind you of the problem

demonstration

Deadlock troubleshooting

/** *  Demonstrates thread deadlock problems  */
public class ThreadDeadLock {
    

    public static void main(String[] args) {
    

        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        new Thread(){
    
            @Override
            public void run() {
    

                synchronized (s1){
    

                    s1.append("a");
                    s2.append("1");

                    try {
    
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
    
                        e.printStackTrace();
                    }

                    synchronized (s2){
    
                        s1.append("b");
                        s2.append("2");

                        System.out.println(s1);
                        System.out.println(s2);
                    }

                }

            }
        }.start();


        new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                synchronized (s2){
    

                    s1.append("c");
                    s2.append("3");

                    try {
    
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
    
                        e.printStackTrace();
                    }

                    synchronized (s1){
    
                        s1.append("d");
                        s2.append("4");

                        System.out.println(s1);
                        System.out.println(s2);
                    }
                }
            }
        }).start();

        try {
    
            Thread.sleep(1000);
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        }

        new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                Map<Thread, StackTraceElement[]> all = Thread.getAllStackTraces();// Track all threads in the current process 
                Set<Map.Entry<Thread, StackTraceElement[]>> entries = all.entrySet();
                for(Map.Entry<Thread, StackTraceElement[]> en : entries){
    
                    Thread t = en.getKey();
                    StackTraceElement[] v = en.getValue();
                    System.out.println("【Thread name is :" + t.getName() + "】");
                    for(StackTraceElement s : v){
    
                        System.out.println("\t" + s.toString());
                    }
                }
            }
        }).start();
    }


}

explain , The first thread gets s1 lock , Get more s2 lock , The second thread gets s2 lock , Get more s1 lock , In this way, it is easy to deadlock

Check with command

Start the above code

image.png

image.png

This prints the thread related information ,

The two threads identified by the arrow are blocked , It's because of the deadlock ,
image.png

Then it shows deadlock 了 .
image.png

Thread sleep troubleshooting

public class TreadSleepTest {
    
    public static void main(String[] args) {
    
        System.out.println("hello - 1");
        try {
    
            Thread.sleep(1000 * 60 * 10);
        } catch (InterruptedException e) {
    
            e.printStackTrace();
        }

        System.out.println("hello - 2");
    }
}

The above code will sleep 10 minute . Let the code not execute

Check with command

Activate the above main Method

image.png

You can see main Thread is time waiting Status quo .
image.png

Multithreading synchronization problem

/** *  Demonstrate thread synchronization  */
public class ThreadSyncTest {
    
    public static void main(String[] args) {
    
        Number number = new Number();
        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);

        t1.setName(" Threads 1");
        t2.setName(" Threads 2");

        t1.start();
        t2.start();
    }
}

class Number implements Runnable {
    
    private int number = 1;

    @Override
    public void run() {
    
        while (true) {
    
            synchronized (this) {
    

                if (number <= 100) {
    

                    try {
    
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
    
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;

                } else {
    
                    break;
                }
            }
        }
    }

}

The above code shows , Create two Number Threads , Then go and give it to me number Variable self increasing , The lock object is this, This is synchronization

image.png

原网站

版权声明
本文为[Zhang Junjie 1994]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070046516690.html