In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use jstack to analyze thread state, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.
Top command
In the linux environment, you can view the cpu usage of each process through the top command, which is sorted by cpu usage by default
1. As can be seen in the above figure, the java process with a pid of 23344 takes up more cpu resources.
2. You can view the cpu usage of each thread under the process through top-Hp 23344.
As you can see in the figure above, a thread with a pid of 25077 accounts for more cpu resources, and the jstack command can be used to continue to view the current stack state of the thread.
Jstack command
After navigating to a thread with high cpu occupancy through the top command, continue to use the jstack pid command to view the stack status of the current java process
The thread dump information generated by the jstack command contains all the living threads in the JVM. In order to analyze the specified thread, we must find out the call stack of the corresponding thread.
In the top command, you have obtained the thread pid that takes up more cpu resources, and convert the pid to a hexadecimal value. Each thread has a nid in thread dump, and you can find the corresponding nid. Execute the stack command to get the thread dump once in a while to distinguish whether there is any difference between the two dump. In the thread call stack of nid=0x246c, it is found that the thread has been executing the calculate method in line 33 of the JstackCase class. If you get this information, you can check whether there is something wrong with the corresponding code.
Analyze thread state through thread dump
In addition to the above analysis, in most cases, the current operation of each thread is analyzed based on thead dump, such as whether there is a deadlock, whether there is a thread holding the lock for a long time, and so on.
In dump, threads generally have the following states:
1. RUNNABLE, the thread is executing
2. BLOCKED, thread is blocked
3. WAITING, the thread is waiting
Example 1: multi-thread contention for synchronized lock
It is obvious: thread 1 acquires the lock, is in the RUNNABLE state, and thread 2 is in the BLOCK state
1. Locked indicates that thread 1 locks the object whose address is 0x000000076bf62208.
2. Waiting to lock indicates that thread 2 is waiting for a lock on the 0x000000076bf62208 object.
3. Waiting for monitor entry [0x000000001e21f000] indicates that thread 1 enters the critical section of the monitor through the synchronized keyword and is in the "Entry Set" queue, waiting for monitor. For more information, please see the JVM implementation of synchronized.
Example 2: suspending threads through wait
Static class Task implements Runnable {@ Override public void run () {synchronized (lock) {try {lock.wait (); / / TimeUnit.SECONDS.sleep (100000);} catch (InterruptedException e) {e.printStackTrace ();}
Dump result
Threads 1 and 2 are in the WAITING state
1. Threads 1 and 2 are locked first, then waiting on. The reason for locking the same object first is that the wait method needs to obtain the monitor of the address object through synchronized first.
2. Waiting on states that after the thread executes the wait method, it releases the monitor, enters the "Wait Set" queue, waits for other threads to execute the notify method with the address of 0x000000076bf62500 object, and wakes itself up. For specific implementation, please refer to the in-depth analysis of the Object.wait/notify implementation mechanism.
This is the answer to the question about how to analyze the thread state with jstack. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
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.