当线程在执行长时间任务的某个循环中时,如何中断该线程? [英] How to interrupt the Thread when it is inside some loop doing long task?
问题描述
可能重复::您如何杀死Java中的线程?
我需要通过将中断信号发送到线程来停止执行一些大任务.我正在使用java.util.concurrent.*中的大多数API.我的任务被发送到线程并执行.该任务来自客户端,因此我对该代码没有任何控制权.
任务类似于:
public class Task1 extends Thread {
public void run() {
while(true){
if(Thread.interrupted()){
return;
}
for(int i=0; i<Integer.MAX_VALUE; i++){
System.out.println("I am task 1 " + i);
}
}
}
};
我希望当它收到中断信号时基本上停止通过for循环(请注意,由于来自客户端,因此无法将Thread.interrputed()逻辑放入for循环中.)我有另一个类使用执行程序执行此任务的计算机.
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是:
public class Task2 extends Thread{
public void run() {
while(true){
if(Thread.interrupted()){
return;
}
System.out.println("I am task 2");
}
}
};
Task2被中断,但是Task1从未中断,并继续在for循环内执行.我不能在客户端代码中放入任何逻辑(类似于for循环).我需要SO社区的帮助来解决此问题.谢谢.
唯一的防弹解决方案是在隔离中运行客户端提供的代码.隔离实际上是Java应用程序可以创建,管理和通信的私有虚拟机.特别是,父应用程序可以安全地杀死隔离对象及其所有线程.
参考: JSR-000121应用程序隔离API规范-最终发布 >
问题在于找到支持隔离的JVM.
Possible Duplicate: How do you kill a thread in Java?
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<Integer.MAX_VALUE; 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.
Reference: JSR-000121 Application Isolation API Specification - Final Release
The problem is finding a JVM that supports Isolates.
这篇关于当线程在执行长时间任务的某个循环中时,如何中断该线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!