欢迎使用普元产品知识库,本知识库包含普元应用开发平台EOSPlatform,流程平台BPS,企业服务总线ESB,微服务平台Microservice,运维管理平台Devops,数据集成平台DI
...
方案二: 应用程序中监听程序关闭,手动回收自己的线程。
示例代码如下:
public void killThread() {
// 当前线程所在的线程组
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
// 线程组中活跃的线程数
int threadCount = currentGroup.activeCount();
Thread[] threads = new Thread[threadCount];
// 拿到线程组下面的所有线程
currentGroup.enumerate(threads);
// 回收线程
for (int i = 0; i < threads.length; i++) {
Thread Thread thread = threads[i];
if if (thread == null) {
continue;
}i
f if (Thread.currentThread() != thread &&
!"main".equals(thread.getName())) {
thread.interrupt();
thread.stop();
}
}
}