In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use jstack to analyze thread state in Java, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What is jstck? This is a tool that Oracle JDK includes by default to print current thread stack information for executing Java processes. The official introduction is as follows:
Jstack prints Java stack traces of Java threads for a given Java process or core file or a remote debug server. For each Java frame, the full class name, method name, 'bci' (byte code index) and line number, if available, are printed.
Note a few key points: the full class name of each Java Frame, the method name, and the line number if you can get it. Friends who have read the previous article on debugging techniques (debugging techniques that 80% of programmers don't know) may remember that one of the functions is Drop Frame to implement backward execution. This is the same place as here, which corresponds to a first-level call in the thread.
The message typed using jstack is basically the same as the printStackTrace when an application encounters an exception, except that it is only a thread call chain. Here, all threads in the application can be typed out through the tool.
Usage
It is used in a manner similar to that of general Java analysis tools, both through the
Command name + pid (process id)
This format is used. For example, for jstack, you can generally apply pid directly to jstack. Here, pid can be obtained through the jps tool of Java, the ps tool under Linux and the task manager under Windows.
Output
Let's take a Tomcat process as an example and the output looks like this:
Let's look at the boxes above:
On the top left is the thread name of the current thread, which can be used to find the operations that our concerned threads are performing among a large number of threads in the application. For example, Tomcat general http-port-x thread is the request processing thread, when the page response is slow, you can directly find this kind of thread.
As the number of requests increases, so does the number of threads. So in general multithreaded application development, a good practical suggestion is to give the created thread a meaningful name, otherwise there are a large number of thread-1 and thread-2 in the stack, God knows which one is yours.
The box content of the second line indicates the execution status of the current thread, whether it is running or TIME_WAITING, or waiting for a lock, etc., which can be understood according to the thread state.
The content in the third big box, just like the stackTrace we output in case of an exception, is the call chain of the current code.
The content in the fourth box is the situation of the lock hung by the current thread.
The screenshot above is the output without locks occupying each other. If there is a lock waiting in a multithread, there will be output like this:
Notice that the thread state becomes BLOCKED, and at the same time, in the thread's call chain, there is an output of waiting to lock, and in the thread holding the lock below, there will be a lock xxx, which is the current lock object, from which you can see which threads are currently waiting for the same lock.
So back to the previous problem, if it is caused by lock occupation at this time, you can see it in the output. At the same time, if the database connection pool is full, the thread will stop on the database connection operation, which can be seen at a glance in stackTrace, and even waiting for network Socket reading will be reflected in the call chain, so you can quickly locate and solve the problem.
The above is how to use jstack to analyze thread state in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.