Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java get thread state and stack information

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to get thread state and stack information from Java". In the operation of actual cases, many people will encounter such a dilemma, 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!

Basic concept

When there is a memory leak or slow running scenario, and sometimes the problem cannot be seen directly from the business log, you need to analyze the jvm memory and thread stack.

The thread stack information mainly records the thread execution of the jvm thread at a certain time. Analyzing the thread state can trace the memory stack information where there is something wrong with the program. The memory stack information is mainly used to record the use of objects in the jvm heap at a certain time. It is mainly used to track which object takes up too much space, thus tracking the thread information that caused the memory leak to see the current number of threads.

Actuator

1.x

Http://host:port/metrics/threads / / current process threads http://host:port/metrics/threads.daemon / / current process background resident threads http://host:port/metrics/threads.peak / / Peak number of current process threads

2.x

Http://host:port/actuator/metrics/jvm.threads.daemon / / current process background resident threads http://host:port/actuator/metrics/jvm.threads.live / / current process threads http://host:port/actuator/metrics/jvm.threads.peak / / peak number of current process threads

Hystrix thread status

If you are connected to turbine, you can view the status of the entire cluster directly through turbine.

When the cluster is large, you only want to see the status of the hystrix thread pool, which can be obtained separately from the hystrix monitoring statistics class.

Http://host:port/sys/hystrix/threads

The source code is as follows

Import com.alibaba.fastjson.JSON;import com.netflix.hystrix.HystrixThreadPoolMetrics;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jmx.export.annotation.ManagedResource;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.stream.Collectors / * * @ author yugj * @ date 22:17 on 19-5-5. * / @ RestController@RequestMapping (path = "/ sys/hystrix") @ ManagedResource (description = "hystrix Endpoint") @ EnableSchedulingpublic class HystrixThreadPoolEndpoint {private boolean showStats = false; private static final Logger log = LoggerFactory.getLogger (HystrixThreadPoolEndpoint.class) @ GetMapping (value = "/ threads") public List threadStats () {return HystrixThreadPoolMetrics.getInstances () .stream () .map ((m)-> {final HystrixThreadStats stats = new HystrixThreadStats (); stats.poolName = m.getThreadPoolKey () .name (); stats.cumulativeExecuted = m.getCumulativeCountThreadsExecuted (); stats.currentActiveCount = m.getCurrentActiveCount (). IntValue () Stats.currentCompletedCount = m.getCurrentCompletedTaskCount (). IntValue (); stats.currentCorePoolSize = m.getCurrentCorePoolSize (). IntValue (); stats.currentLargestPoolSize = m.getCurrentLargestPoolSize (). IntValue (); stats.currentMaxPoolSize = m.getCurrentMaximumPoolSize (). IntValue (); stats.currentPoolSize = m.getCurrentPoolSize (). IntValue (); stats.currentQueueSize = m.getCurrentQueueSize (). IntValue () Stats.currentTaskCount = m.getCurrentTaskCount (). IntValue (); return stats;}) .clients (Collectors.toList ());} @ GetMapping (value = "/ setShowStats") public String setShowStats (Boolean showStats) {if (showStats! = null) {this.showStats = showStats;} return "this.show stats:" + this.showStats } @ Scheduled (fixedRate = 5000) public void showStats () {if (showStats) {List statsList = threadStats (); log.info ("thread stats: {}", JSON.toJSONString (statsList));}} class HystrixThreadStats {private String poolName; private Long cumulativeExecuted; private Integer currentActiveCount; private Integer currentCompletedCount; private Integer currentCorePoolSize; private Integer currentLargestPoolSize Private Integer currentMaxPoolSize; private Integer currentPoolSize; private Integer currentQueueSize; private Integer currentTaskCount; public String getPoolName () {return poolName;} public void setPoolName (String poolName) {this.poolName = poolName;} public Long getCumulativeExecuted () {return cumulativeExecuted } public void setCumulativeExecuted (Long cumulativeExecuted) {this.cumulativeExecuted = cumulativeExecuted;} public Integer getCurrentActiveCount () {return currentActiveCount;} public void setCurrentActiveCount (Integer currentActiveCount) {this.currentActiveCount = currentActiveCount;} public Integer getCurrentCompletedCount () {return currentCompletedCount } public void setCurrentCompletedCount (Integer currentCompletedCount) {this.currentCompletedCount = currentCompletedCount;} public Integer getCurrentCorePoolSize () {return currentCorePoolSize;} public void setCurrentCorePoolSize (Integer currentCorePoolSize) {this.currentCorePoolSize = currentCorePoolSize;} public Integer getCurrentLargestPoolSize () {return currentLargestPoolSize } public void setCurrentLargestPoolSize (Integer currentLargestPoolSize) {this.currentLargestPoolSize = currentLargestPoolSize;} public Integer getCurrentMaxPoolSize () {return currentMaxPoolSize;} public void setCurrentMaxPoolSize (Integer currentMaxPoolSize) {this.currentMaxPoolSize = currentMaxPoolSize;} public Integer getCurrentPoolSize () {return currentPoolSize } public void setCurrentPoolSize (Integer currentPoolSize) {this.currentPoolSize = currentPoolSize;} public Integer getCurrentQueueSize () {return currentQueueSize;} public void setCurrentQueueSize (Integer currentQueueSize) {this.currentQueueSize = currentQueueSize;} public Integer getCurrentTaskCount () {return currentTaskCount } public void setCurrentTaskCount (Integer currentTaskCount) {this.currentTaskCount = currentTaskCount;}

Linux

Ps huH p {pid} | wc-ljstack generate thread stack

When the service cup soars or something goes wrong and needs to be located at the host level, use the top-c command to see which process takes up too much resources.

Find processes with high resource consumption

The process that clearly needs to be located finds the corresponding process id with the following command

Ps aux | grep {application alias}

You can locate a specific high load thread with the following command:

Query which thread occupies a high loadtop-Hp {process pid} thread id converts hexadecimal format to hexadecimal value printf% x {thread pid} specify a specific number of rows stack information jstack {process id} | grep-A 200 {thread id}

Next, export the corresponding thread stack through jstack

The corresponding parameters of jstack are as follows

-m to print both java and native frames (mixed mode)

-l long listing. Prints additional information about locks

The server has relatively many threads and a large file size, so it is generally not considered to look at the server. In addition, such a check will also lead to the neglect of some statistical information.

Export the file and download it locally by using the following command

Jstack-l {pid} > > {dump-file-path}

The docker environment involves some permissions, which need to be executed in docker. According to the actual situation, the id of the process in docker will generally contact the operation and maintenance staff.

This is the end of "how to get thread status and stack information from 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report