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_Wait - Fatal编程技术网

Java 如何在等待中终止正在运行的线程?

Java 如何在等待中终止正在运行的线程?,java,multithreading,wait,Java,Multithreading,Wait,当我试图杀死我的Robber线程时,有些线程会死亡,但有些线程会卡在wait()块中,有什么更好的方法可以杀死所有线程,或者如何让被阻止的线程被杀死 private int robberId; private static int robberGlobalId=0; private TreasureChest chest; private boolean alive = true; public Robber(TreasureChest chest) { robberId = robb

当我试图杀死我的Robber线程时,有些线程会死亡,但有些线程会卡在wait()块中,有什么更好的方法可以杀死所有线程,或者如何让被阻止的线程被杀死

private int robberId;
private static int robberGlobalId=0;
private TreasureChest chest;
private boolean alive = true;

public Robber(TreasureChest chest) {
    robberId = robberGlobalId;
    robberGlobalId++;

    this.chest = chest;
}

public void run() {
    while (alive) {
        try {
            synchronized(chest){
                robCoin();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("Robber " +robberId +" just died");
}

public void robCoin() throws InterruptedException {
    if (chest.getTreasureAmount() <= 0 ) {
        chest.wait();
    } else { 
        chest.removeCoin();
    }
    Thread.sleep(50);
}

public void killRobber() {
    alive = false;
}
private-id;
私有静态int-globalid=0;
私人宝库;
private boolean alive=true;
盗贼(宝库){
robberId=robberGlobalId;
globalid++;
这个.胸部=胸部;
}
公开募捐{
(活着时){
试一试{
同步(胸部){
robCoin();
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
System.out.println(“强盗”+强盗ID+“刚死”);
}
public void robCoin()抛出InterruptedException{
如果(胸部getTreasureAmount()
当我试图杀死我的强盗线程时,有些线程会死亡,但有些线程会卡在wait()块中,有什么更好的方法可以杀死所有线程

“杀死”线程的正确方法是使用
thread.interrupt()
中断它。如果线程在
等待中被阻塞(…)
call,这将立即抛出
InterruptedException
。当捕获
InterruptedException
时,最好立即重新中断线程以保留中断标志,因为抛出异常时,中断位被清除

try {
    ...wait();
} catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
    // handle the interrupt
    return;
}
由于并非所有方法都抛出InterruptedException
,因此您还可以检查以确保线程已被中断,如下所示:

if (Thread.currentThread().isInterrupted()) {
    // stop processing
    return;
}
或者在您的情况下:

while (alive && !Thread.currentThread().isInterrupted()) {

顺便说一句,
alive
应该是
volatile
,因为它看起来可以被多个线程访问。

中断线程是一种方法,正如@Gray的回答所示,但是当你“杀死”强盗而不是中断他们时,唤醒等待的线程可能会更干净

在下面的示例中,“Robber任务”(通过
run()
方法实现)将一直等待,直到Robber还活着并且箱子是空的(小于或等于0)。如果调用了
killRobber()
,等待线程将被唤醒并优雅地退出
run()
(活着将是
false

public void run(){
试一试{
同步(胸部){
while(cost.getTreasureAmount()
public void run() {
    try{
        synchronized(chest){
            while (chest.getTreasureAmount() <= 0 && alive) {
                chest.wait();
            } 
            if(alive){
                chest.removeCoin();
            }
        }
    }catch (InterruptedException ie){
        /* Thread interrupted do something appropriate,
           which may be to do nothing */
    }
}

public void killRobber() {
    synchronized(chest){
        alive = false;
        chest.notifyAll();
    }
}