Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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_Interrupt - Fatal编程技术网

Java 如何在线程被中断后完全停止它?

Java 如何在线程被中断后完全停止它?,java,multithreading,interrupt,Java,Multithreading,Interrupt,在我当前的代码中,我正在创建一个实现runnable的线程。我从主界面开始,然后让主界面打断它。但是,一旦中断,它将继续运行。我想让线完全停止 螺纹等级: public class MyThread implements Runnable { @Override public void run() { // string relating to the threads name // Note that you actually set the

在我当前的代码中,我正在创建一个实现
runnable
的线程。我从主界面开始,然后让主界面打断它。但是,一旦中断,它将继续运行。我想让线完全停止

螺纹等级:

public class MyThread implements Runnable {

    @Override
    public void run() {

        // string relating to the threads name
        // Note that you actually set the threads name in the main
        String threadName = Thread.currentThread().getName();


        System.out.println(threadName + " is now started...");

        System.out.println(threadName + " about to count down...");

        for (int loop = 100; loop > 1; loop--) {

            try {           
                //for 0.5 seconds
                Thread.sleep(500);      
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(threadName + " was Interupted!");        
            }               

            System.out.println(loop);
        }           

    }

}
主要内容:

输出(注意线程如何不停止):

只需正确编写run()方法,这样它在被中断时就可以脱离循环,而不仅仅是继续

线程确实有一个stop()方法,但由于某种原因它被弃用;它会使事物处于不一致和潜在的不稳定状态。不要用它

只需正确编写run()方法,使其在中断时脱离循环,而不仅仅是继续


线程确实有一个stop()方法,但由于某种原因它被弃用;它会使事物处于不一致和潜在的不稳定状态。不要用它

只是
break
退出循环:

catch (InterruptedException e) {
    e.printStackTrace();
    System.out.println(threadName + " was Interupted!");
    Thread.currrentThread().interrupt();
    break;
}

只需将
从循环中断开即可:

catch (InterruptedException e) {
    e.printStackTrace();
    System.out.println(threadName + " was Interupted!");
    Thread.currrentThread().interrupt();
    break;
}

只需使用一个
boolean
标志,在
run()
方法中执行任何操作之前,将检查该标志,并根据其状态执行所需的操作。确保它应该是易变的

只需调用
myt.setRunning(false)
而不是
t.interrupt()

样本cde:

class MyThread implements Runnable {

    private volatile boolean isRunning=true;

    @Override
    public void run() {             

        for (int loop = 100; loop > 1; loop--) {                 

            if(!isRunning){
               // break or do whatever is needed here
            }             
        }    
    }               

}

只需使用一个
boolean
标志,在
run()
方法中执行任何操作之前,将检查该标志,并根据其状态执行所需的操作。确保它应该是易变的

只需调用
myt.setRunning(false)
而不是
t.interrupt()

样本cde:

class MyThread implements Runnable {

    private volatile boolean isRunning=true;

    @Override
    public void run() {             

        for (int loop = 100; loop > 1; loop--) {                 

            if(!isRunning){
               // break or do whatever is needed here
            }             
        }    
    }               

}

对于此类重复任务,您还可以使用:

请注意,此代码不会在零处停止

public class CountdownTask extends TimerTask {

    private int from;

    public CountdownTask(int from) {
        this.from = from;
    }

    @Override
    public void run() {
        System.out.println(from--);
    }
}

private void example() throws InterruptedException {
    Timer timer = new Timer("Countdown");
    timer.schedule(new CountdownTask(100), 0, 500);
    Thread.sleep(2000); //do something
    timer.cancel(); //stop task
}

对于此类重复任务,您还可以使用:

请注意,此代码不会在零处停止

public class CountdownTask extends TimerTask {

    private int from;

    public CountdownTask(int from) {
        this.from = from;
    }

    @Override
    public void run() {
        System.out.println(from--);
    }
}

private void example() throws InterruptedException {
    Timer timer = new Timer("Countdown");
    timer.schedule(new CountdownTask(100), 0, 500);
    Thread.sleep(2000); //do something
    timer.cancel(); //stop task
}

在循环外部捕获中断的异常,以便在
Catch
块停止执行后,不再重复执行该块…您可能还希望在中断后使线程等待,直到其完全停止。在循环外部捕获中断的异常,以便
Catch
块停止执行后,该块不再重复再次重复…您可能还希望在中断线程后等待线程完全停止。
break
很难看,特别是当您可以重写一段代码,而不必使用
break
和wise来执行同样的操作,并且更易于阅读。break没有错,如果你需要清理循环内部,你也应该恢复中断标志,以防你没有通过调用
线程来重新调用
中断异常。currentThread().interrupt()
中断之前
中断
中断
很难看,特别是当你可以重写一段代码,而不需要中断,而且更容易阅读。中断没有错,如果您需要清理循环内部,您还应该通过在
中断之前调用
线程.currentThread().interrupt()
来恢复中断标志,以防您没有重新调用
中断异常。