Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以停止后端线程吗?_Java_Multithreading_Spring Boot - Fatal编程技术网

Java 我们可以停止后端线程吗?

Java 我们可以停止后端线程吗?,java,multithreading,spring-boot,Java,Multithreading,Spring Boot,在我的SpringBoot项目中,我有下面的GetWeb服务 @GetMapping("/testThread") @Transactional public Map<String, Object> testThread(HttpServletRequest request) throws InterruptedException { for (int i=0;i<10;i++){ Thread.sleep(3000); System.out.println(

在我的SpringBoot项目中,我有下面的GetWeb服务

@GetMapping("/testThread")
@Transactional
public Map<String, Object> testThread(HttpServletRequest request) throws InterruptedException {

for (int i=0;i<10;i++){
    Thread.sleep(3000);
    System.out.println(i);
}
    return null;
}
@GetMapping(“/testThread”)
@交易的
公共映射测试线程(HttpServletRequest请求)抛出InterruptedException{

对于(int i=0;i而言,一个简单的解决方案是从testThread方法获取线程id,可能使用方法:

Thread.currentThread().getId()
在此之后,您可以创建另一个Rest控制器,该控制器在输入中接受一个id线程并杀死它。
但这不是一个安全的解决方案

事实上,我不是在谈论线程,我们可以说是它的后台进程,如果我从postman点击rest服务,那么这个循环将启动,5-10秒后我取消请求。那么我们有任何选项来停止这个循环的执行吗?使用ExecutorService并将作业提交给id,生成id和将其与executor service返回的Future关联。使用另一个REST调用根据生成的ID请求取消。@rkosegi能否请您再简单介绍一下,我如何生成ID,如何与executor service关联,以及如何使用另一个REST取消它?如果您能提供一些代码示例,那就太好了s.Kill thread是错误的模式@Parad ok..那么我应该使用stop方法来杀死线程吗?@harish bagora这可能是一个解决方案,但不是一个好的解决方案。这里有一个很好的例子: