场景描述:
用户要在某个页面操作一个服务的运行,另外需要在页面添加停止按钮中断任务继续运行。
- 一开始我就想到了在发起运行时把当前线程放到一个ConCurrentHashMap中,并由前段传一个标记过来,停止的时候也把这个标记传过来,然后找到这个线程停止掉,实现如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* description:<任务开始与结束>
* @author 武亚军
* @version 1.0
* @date 2021-08-28 上午10:25
*/
@Slf4j
@RestController
@RequestMapping("/thread")
public class ThreadeDemoController {
private static Map<String, Thread> concurrentHashMap = new ConcurrentHashMap<>();
@RequestMapping(value = "/run", method = RequestMethod.GET)
public String startExecute(String uuid) {
Thread thread = Thread.currentThread();
concurrentHashMap.put(uuid, thread);
log.info("uuid:{},thread:{}", uuid, thread.getName());
try {
for (int i = 1; i <= 50; i++) {
log.info("当前执行带第{}次", i);
Thread.sleep(2000);
}
} catch (Exception e) {
return "运行失败!";
}
return "运行完成!";
}
@RequestMapping(value = "/stop", method = RequestMethod.GET)
public String stopExecute(String uuid) {
Thread thread = concurrentHashMap.get(uuid);
if (Objects.isNull(thread)) {
return "任务未在运行中";
}
log.info("停止时找到的线程名字:{}", thread.getName());
try {
if (thread.isAlive() && !thread.isInterrupted()) {
thread.interrupt();
log.info("{}线程已经停止,线程状态:{}", thread, thread.isInterrupted());
}
} catch (Exception e) {
log.error("{}线程停止失败", thread);
return "停止运行失败!";
}
return "停止运行成功!";
}
}
但是这样会有一个问题,前端运行接口的调用还在等待结果的返回,但是后台的线程以及被中断了,不会有任何返回了,只能等待请求超时。
- 于是我又想了一个方式,在运行的时候,我去创建一个子线程去运行我的任务,方法的主线程等待子线程的结果,这样即使子线程被中断,主线程还是可以去正常返回结果给前端的,实现如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* description:<任务开始与结束>
*
* @author 武亚军
* @version 1.0
* @date 2021-08-28 上午10:25
*/
@Slf4j
@RestController
@RequestMapping("/thread")
public class ThreadeDemoController {
private static Map<String, Thread> concurrentHashMap = new ConcurrentHashMap<>();
@RequestMapping(value = "/run", method = RequestMethod.GET)
public String startExecute(String uuid) {
try {
MyRunable myRunable = new MyRunable(concurrentHashMap, uuid);
myRunable.run();
} catch (Exception e) {
return "运行结束";
}
// 运行成功之后将当前线程从map中移除
concurrentHashMap.remove(uuid);
return "运行成功";
}
@RequestMapping(value = "/stop", method = RequestMethod.GET)
public String stopExecute(String uuid) {
Thread thread = concurrentHashMap.get(uuid);
if (Objects.isNull(thread)) {
return "任务未在运行中";
}
log.info("停止时找到的线程名字:{}", thread.getName());
try {
if (thread.isAlive() && !thread.isInterrupted()) {
thread.interrupt();
log.info("{}线程已经停止,线程状态:{}", thread, thread.isInterrupted());
}
} catch (Exception e) {
log.error("{}线程停止失败", thread);
return "停止运行失败!";
}
return "停止运行成功!";
}
}
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/**
* description:<运行线程>
*
* @author 武亚军
* @version 1.0
* @date 2021-08-28 上午11:43
*/
@Slf4j
public class MyRunable implements Runnable {
private Map<String, Thread> currentThreadMap;
private String uuid;
public MyRunable(Map<String, Thread> currentThreadMap, String uuid) {
this.currentThreadMap = currentThreadMap;
this.uuid = uuid;
}
@SneakyThrows
@Override
public void run() {
Thread thread = Thread.currentThread();
currentThreadMap.put(uuid, thread);
log.info("uuid:{},thread:{}", uuid, thread.getName());
try {
for (int i = 1; i <= 50; i++) {
log.info("当前执行带第{}次", i);
Thread.sleep(2000);
}
} catch (Exception e) {
throw new Exception("线程运行停止!");
}
}
}