java 执行长事物,当线程在执行长时间任务的某个循环中时,如何中断该线程?...

在Java中,当需要停止一个正在执行的任务时,通常会使用中断信号。然而,在给定的代码示例中,由于客户端代码无法修改,`for`循环内部没有检查中断状态,导致任务无法正常中断。解决这个问题的一种方法是将客户端代码包装在一个可中断的运行环境中,如使用`Future`和`ExecutorService`。示例中`Task1`并未响应中断,而`Task2`正确响应了中断。问题在于如何在不修改`Task1`内部循环的情况下使其响应中断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I need to stop doing some big task by sending the interruption signal to the Thread. I am using most of the APIs from java.util.concurrent.*. My task is send to the Thread and is executed. This task comes from the client so I do not have any control over that code.

Task are something similar to:

public class Task1 extends Thread {

public void run() {

while(true){

if(Thread.interrupted()){

return;

}

for(int i=0; i

System.out.println("I am task 1 " + i);

}

}

}

};

I want to basically stop looping through the for-loop when it receives the interruption signal (Please note that I cannot put the Thread.interrputed() logic inside for-loop as it comes from the client.) I have another class that use the Executor to execute this task.

public class ConcurrentTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

ConcurrentTest test = new ConcurrentTest();

ExecutorService executorService = Executors.newFixedThreadPool(2);

Task1 task1 = new Task1();

Future> runningTask1 = executorService.submit(task1);

Task2 task2 = new Task2();

Future> runningTask2 = executorService.submit(task2);

//Stop First task after 5 second

try {

TimeUnit.SECONDS.sleep(5);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

runningTask1.cancel(Boolean.TRUE);

System.out.println("runningTask1.isDone is " + runningTask1.isDone());

System.out.println("runningTask1.isCancelled is " + runningTask1.isCancelled());

//Stop Second task after 10 second

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedException e){

e.printStackTrace();

}

runningTask2.cancel(Boolean.TRUE);

System.out.println("runningTask2.isDone is " + runningTask2.isDone());

System.out.println("runningTask2.isCancelled is " + runningTask2.isCancelled());

}

}

Task2 is :

public class Task2 extends Thread{

public void run() {

while(true){

if(Thread.interrupted()){

return;

}

System.out.println("I am task 2");

}

}

};

Task2 is interrpted however Task1 is never interrupted and continue executing inside for loop. I cannot put any logic inside client code (something similar to for-loop). I need help from SO community to resolve this issue. Thanks.

解决方案

The only bomb-proof solution is to run the client supplied code in an Isolate. An isolate is essentially a private virtual machine that a Java app can create, managed and communicate with. In particular, the parent app can safely kill an isolate and all its threads.

The problem is finding a JVM that supports Isolates.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值