In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use Jstack in Java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Jstack-Prints Java thread stack traces for a Java process, core file, or remote debug server.
The main function of Jstack is to generate the information of all threads in the current process, that is, the thread snapshot of JVM at the current time. Through the thread information, we can locate the problems in the program, such as long pause, high CPU occupancy rate and so on.
The information in the thread snapshot is a stack collection of methods being executed by each thread in the current java virtual machine. With the stack information, we can analyze where our program problems occur, such as deadlocks between threads, long external resource requests, dead loops and so on.
Use:
Jstack [options] pidjstack [options] executable corejstack [options] [server-id@] remote-hostname-or-IPOPTIONS-F Force a stack dump when jstack [- l] pid does not respond. -l Long listing. Prints additional information about locks such as a list of owned java.util.concurrent ownable synchronizers. See the AbstractOwnableSynchronizer class description at http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/AbstractOwnableSynchronizer.html-m Prints a mixed mode stack trace that has both Java and native Compact + frames.
-F forces stack information to be output when a normal request is not answered.
-l extra print lock information. When a deadlock occurs, you can view the lock information
-m if you call the information of the local method stack, you can print the stack of Candlestick +.
Take an example of a deadlock. Take a look at the information you see using Jstack.
Public class Jstack {private static Object obj1 = new Object (); private static Object obj2 = new Object (); public static void main (String [] args) {new Thread (())-> {synchronized (obj1) {try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace () } synchronized (obj2) {}) .start (); new Thread (()-> {synchronized (obj2) {try {Thread.sleep (2000)) } catch (InterruptedException e) {e.printStackTrace ();} synchronized (obj1) {}) .start ();}}
In the above code, the first thread gets obj1's lock and waits for obj2's lock, and the second thread gets obj2's lock and waits for obj1's lock, so a deadlock occurs.
First get the current process pid through the jps command, and then get the thread information through jstack. You can see that both threads are in a blocking state.
Thread-1 # 12 prio=5 os_prio=0 tid=0x00007fdff871c800 nid=0x3cc2 waiting for monitor entry [0x00007fdfce0fc000] java.lang.Thread.State: BLOCKED (on object monitor) at com.example.demo.jstack.Jstack.lambda$main$1 (Jstack.java:36)-waiting to lock (a java.lang.Object)-locked (a java.lang.Object) at com.example.demo.jstack.Jstack$$Lambda$2/2052001577.run (Unknown Source) at java. Lang.Thread.run (Thread.java:748) "Thread-0" # 11 prio=5 os_prio=0 tid=0x00007fdff871a800 nid=0x3cc1 waiting for monitor entry [0x00007fdfce1fc000] java.lang.Thread.State: BLOCKED (on object monitor) at com.example.demo.jstack.Jstack.lambda$main$0 (Jstack.java:25)-waiting to lock (a java.lang.Object)-locked (a java.lang.Object) at com.example.demo.jstack.Jstack$$Lambda$1/1174361318. Run (Unknown Source) at java.lang.Thread.run (Thread.java:748)
The first line displays information such as thread name, thread priority, thread id, thread state description, etc.
The second line shows the status of the current thread
The state of a thread in Java is divided into NEW, RUNNABLE, BLOCKED, WATING, TIMED_WATING, and TERMINATED, but the NEW state does not appear in the snapshot.
Below is the information about the call stack of the current thread. The call stack contains information about the lock.
Locked indicates that the owner of the monitor successfully applied for a lock using synchronized.
Waiting to lock indicates that the application object lock using synchronized was not successful and entered the waiting area.
Waiting on indicates that after successfully applying for an object lock with synchronized, the wait method is called to enter the waiting area of the object and wait.
Parking to wait for park is the basic thread blocking primitive that does not block on objects through the monitor. The new mechanism that appears with the concurrent package is different from the synchronized system.
At the end, it also shows that there is a deadlock in the code.
Found one Java-level deadlock:== "Thread-1": waiting to lock monitor 0x00007fdfac006638 (object 0x000000076e925a90, a java.lang.Object), which is held by "Thread-0"Thread-0": waiting to lock monitor 0x00007fdfac003da8 (object 0x000000076e925aa0, a java.lang.Object) Which is held by "Thread-1" Java stack information for the threads listed above:=== "Thread-1": at com.example.demo.jstack.Jstack.lambda$main$1 (Jstack.java:36)-waiting to lock (a java.lang.Object)-locked (a java.lang.Object) at com.example.demo.jstack.Jstack$$Lambda$2/2052001577.run (Unknown Source) at java.lang.Thread.run (Thread.java: Thread-0: at com.example.demo.jstack.Jstack.lambda$main$0 (Jstack.java:25)-waiting to lock (a java.lang.Object)-locked (a java.lang.Object) at com.example.demo.jstack.Jstack$$Lambda$1/1174361318.run (Unknown Source) at java.lang.Thread.run (Thread.java:748) Found 1 deadlock.
All right, now that we're familiar with Jstack, we use an endless loop of code to navigate through Jstack to make CPU occupy 100% of the lines of code.
Public class JstackDemo {public static Executor executor = Executors.newFixedThreadPool (3); private static Object lock = new Object (); public static void main (String [] args) {Task task1 = new Task (); Task task2 = new Task (); executor.execute (task1); executor.execute (task2) } public static class Task implements Runnable {@ Override public void run () {synchronized (lock) {run0 ();}} private void run0 () {int I = 0; while (true) {ionization;}
1. First, check the process id that occupies 100% of CPU through top.
2. Use the top-Hp process id to view the thread id that takes up the most CPU
3. Convert the thread id to hexadecimal
17997-> 464d
4. Use Jstack to view the process where Java is located, and find the corresponding thread
This is the end of the content of "how to use Jstack in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.