gpt4 book ai didi

java - 停止已经运行的线程

转载 作者:行者123 更新时间:2023-11-29 04:16:07 25 4
gpt4 key购买 nike

我有两个 API:一个启动线程,另一个停止线程。我可以通过调用 /start API 成功地启动一个线程,但是我无法通过调用 /stop API 来停止已经运行的线程。似乎 Executor#stop() 什么都不做。

我的 RestController:

@Autowired
private Executor executor;

@RequestMapping(path = "/start", method = GET)
public ResponseEntity<HttpStatus> startLongTask() {
executor.start();
return ResponseEntity.ok(HttpStatus.OK);
}

@RequestMapping(path = "/stop", method = GET)
public ResponseEntity<HttpStatus> stopLongTask() {
executor.stop();
return ResponseEntity.ok(HttpStatus.OK);
}

我的执行者:

@Component
public class Executor {

@Value("${threads.number}")
private int threadsNumber;

private ExecutorService executorService;

@Autowired
private OtherService otherService;

@PostConstruct
private void init() {
executorService = Executors.newFixedThreadPool(threadsNumber);
executorService = Executors.newScheduledThreadPool(threadsNumber);
}

/**
* Start.
*/
public void start() {
executorService.submit(() -> otherService.methodImExecuting());
}

/**
* Stop.
*/
@PreDestroy
publicvoid stop() {
executorService.shutdownNow();
try {
if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
executorService.shutdownNow();
}
}
}

这是methodImExecuting:

@Component
public class OtherService {

public void methodImExecuting() {
List<SomeObject> dataList = repository.getDataThatNeedsToBeFilled();
for (SomeObject someObject : dataList) {
gatewayService.sendDataToOtherResourceViaHttp(someObject);
}
}
}

最佳答案

简短回答:您无法停止不合作的正在运行的线程。线程有一个已弃用的 destroy() 方法,但这会导致您的 VM 处于“不良”状态。

干净地结束线程的唯一可能性是中断它。但是检查中断是线程本身的任务。

所以你的methodImExcecuting应该看起来像:

void methodImExecuting() throws InterruptedException {
// it depends on your implementation, I assume here that you iterate
// over a collection for example
int loopCount = 0;
for (Foo foo : foos) {
++loopCount;
if (loopCount % 100 == 0) {
if (Thread.interrupted())
throw new InterruptedException();
}
...
}

这取决于您的实现,如果您的线程被中断,您必须多久查看一次。但事实是,executorService.shutdownNow(); 的调用只会设置 executorService 当前运行的所有线程的 interrupted 标志。要真正中断线程,线程必须自己检查是否设置了 interrupted 标志,然后抛出 InterruptedException

关于java - 停止已经运行的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com