前端页面Ajax控制后端线程(Thread)运行

1、后端生成线程 

JSONObject redisJson = new JSONObject();
Thread th = new Thread(
() -> {
...
while() {
...
}
...
}
);
th.start();
//以下内容为重点:需借用 redis 存储当前线程名
redisJson .put("thread_name",th.getName())
redisService.setValue(taskKey, StringUtils.toStringTrim(redisJson));
return redisJson

 

2、前端调用Ajax

JSONObject redisJson = JSONObject.parseObject(redisService.getValue("ekg:meta:zcwj:extractparagraphfile:" + taskId));
String th_name = StringUtils.toStringTrim(redisJson.get("thread"));
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int noThreads = currentGroup.activeCount();
Thread[] lstThreads = new Thread[noThreads];
currentGroup.enumerate(lstThreads);
System.err.println("现有线程数" + noThreads);
for (int i = 0; i < noThreads; i++) {
String nm = lstThreads[i].getName();
System.err.println("线程号:" + i + " = " + nm);
if (nm.equals(th_name)) {
System.err.println("删除线程:" + i + " = " + th_name);
lstThreads[i].interrupt();
lstThreads[i].stop();
}
}
redisService.removeKey("ekg:meta:zcwj:extractparagraphfile:" + taskId);
//循环所有线程,根据线程名称,关闭当前制定的线程







posted @ 2022-12-13 13:04  糖炒栗  阅读(90)  评论(0编辑  收藏  举报