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()
来恢复中断标志,以防您没有重新调用中断异常。