add thread statistics (base)

This commit is contained in:
huoyo 2023-03-17 23:23:30 +08:00
parent 8bae6c2954
commit dc59f45600
3 changed files with 128 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import cn.langpy.kotime.model.*;
import cn.langpy.kotime.service.ClassService;
import cn.langpy.kotime.service.GraphService;
import cn.langpy.kotime.service.SysUsageService;
import cn.langpy.kotime.service.ThreadUsageService;
import cn.langpy.kotime.util.Context;
import cn.langpy.kotime.util.InvalidAuthInfoException;
import cn.langpy.kotime.util.KoUtil;
@ -358,4 +359,13 @@ public class KoTimeController {
graphService.clearAll();
return true;
}
@GetMapping("/getThreadsInfo")
@ResponseBody
@Auth
public List getThreadsInfo() {
ThreadUsageService usageService = ThreadUsageService.newInstance();
List<ThreadInfo> threads = usageService.getThreads();
return threads;
}
}

View File

@ -0,0 +1,78 @@
package cn.langpy.kotime.model;
import java.util.List;
public class ThreadInfo {
private Long id;
private String name;
private String classType;
private String state;
private Boolean isInterrupted;
private Boolean isDaemon;
private Integer priority;
private List<StackTraceElement> stacks;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getInterrupted() {
return isInterrupted;
}
public void setInterrupted(Boolean interrupted) {
isInterrupted = interrupted;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Boolean getDaemon() {
return isDaemon;
}
public void setDaemon(Boolean daemon) {
isDaemon = daemon;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public List<StackTraceElement> getStacks() {
return stacks;
}
public void setStacks(List<StackTraceElement> stacks) {
this.stacks = stacks;
}
}

View File

@ -0,0 +1,40 @@
package cn.langpy.kotime.service;
import cn.langpy.kotime.model.ThreadInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class ThreadUsageService {
private static Logger log = Logger.getLogger(ThreadUsageService.class.toString());
public static ThreadUsageService newInstance() {
return new ThreadUsageService();
}
public List<ThreadInfo> getThreads() {
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
int activeCount = threadGroup.activeCount();
Thread[] threads = new Thread[activeCount];
threadGroup.enumerate(threads);
List<ThreadInfo> list = new ArrayList<>();
for (int i = 0; i < activeCount; i++) {
Thread thread = threads[i];
ThreadInfo threadInfo = new ThreadInfo();
threadInfo.setId(thread.getId());
threadInfo.setName(thread.getName());
threadInfo.setClassType(thread.getClass().getSimpleName());
threadInfo.setState(thread.getState().name());
threadInfo.setInterrupted(thread.isInterrupted());
threadInfo.setDaemon(thread.isDaemon());
threadInfo.setPriority(thread.getPriority());
StackTraceElement[] stackTrace = thread.getStackTrace();
threadInfo.setStacks(Arrays.stream(stackTrace).collect(Collectors.toList()));
list.add(threadInfo);
}
return list;
}
}