Java Thread.stop()vs Thread.interrupt()
我有以下代码:
renderThread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10000000; i++) { System.out.println("Number: " + i); } } }); renderThread.start(); try { Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); } renderThread.interrupt(); //It should stop, but it does not stop.
renderThread.interrupt()
不会中断线程,它会继续运行。 如果我用renderThread.interrupt()
替换.stop()
,则线程停止。 但是,不推荐使用.stop()
。 那么,如果不推荐stop
并且interrupt
不起作用,那么stop
线程的正确方法是什么?
当你调用interrupt()时,它会触发一个布尔标志,告诉它应该停止的运行函数。 这就是我经常写这样的运行函数的原因。
void run() { while(!Thread.interrupted()) { //Do something } }
仅仅因为你调用中断并不意味着线程会立即停止或完全停止。 这取决于run函数中的内容。
要真正说明为什么你不应该使用Thread.stop()
?
Doug Lea最近提到Thread.stop将是Java第一个实现Java的方法 。
他对某人的评论作出回应:
另外,另一个同步的事情是它处理线程的异步中止(即thread.stop())。 我知道stop()已被弃用,但你永远都不知道。
是:
但你知道! 从JDK8开始,Thread.stop真的不见了。 这是第一个被弃用的方法。 它现在只抛出UnsupportedOperationException。
http://cs.oswego.edu/pipermail/concurrency-interest/2013-December/012028.html
你应该看看这篇文章: http :/ 10/ 10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/
基本上你不应该停止一个线程(不安全),但要中断它。 你缺少的是让线程处理中断。
你需要做的就是中断当前线程或终止线程的运行过程是在catch块中添加下面的语句
try { Thread.sleep(4000); System.out.println("VAS CONSULTING") } catch(InterruptedException e) { //Stop printing out VAS CONSULTING return; /*This will terminate the thread*/ }
如果在run方法中没有使用不抛出Interrupted Exception的方法,你可以这样做来处理Thread Interruption或终止Thread
@Override public void run() { System.out.println("VAS CONSULTING"); if(Thread.Interrupted()) { doSomethingElse();//process something else within the thread return; //Terminates the current Thread } }
我希望这可以帮助你或其他人。
renderThread.interrupt()不会中断线程,它会继续运行。
- 如果线程正在执行并且调用了
interrupt()
则设置中断状态。 - 如果在调用Object类的
wait
方法或此类的Thread.join
和Thread.sleep
方法之一中阻止此线程,则其中断状态将被清除 ,并且它将收到InterruptedException
。
例如:
Thread t = new Thread() { public void run() { try { for(int i=0; i<10000;i++) System.out.println("Is interrupTed? "+this.isInterrupted()); Thread.sleep(200); } catch (InterruptedException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } } }; t.start(); t.interrupt(); }
上面的代码,我使用了for
循环来拦截带有isInterrupted()
函数的interrput标志并打印出来。 当调用t.interrupt()
, isInterrupted()
将返回true
,您将在控制台中看到它。 一旦到达Thread.sleep(miliTime)
您将看到捕获的InterruptedException
如上所述被抛出。
停止线程:
你可以使用这个isInterrupted()
标志检查是否在它上面调用了interrupt()
并从执行返回:
例如:
Thread t = new Thread() { public void run() { long executeTime = System.currentTimeMillis() ; int i = 0; for(; i<10000 && !this.isInterrupted();i++) System.out.println("Is interrupted? "+this.isInterrupted()); System.out.println("Was Interrupted? "+this.isInterrupted() +" after cycle: "+ i); System.out.println("Time it gets Executed: " +(System.currentTimeMillis() - executeTime+" ms")); } }; t.start(); try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } t.interrupt();
在运行上面的代码时:我已经将主线程发送到静态主函数内的200
睡眠状态,以允许启动的线程t
运行一段时间。 然后我调用了
t.interrupt()
导致for
循环停止并打印输出如下:
Was Interrupted? true after cycle: 1477 Time it gets Executed: 258 ms
这表明for
循环在t.interrupt();
之前有1148
次迭代t.interrupt();
在main
函数中调用。
有关更多详细信息,请阅读Thread.interrupt()
函数的文档。
interrupt()方法不会停止Thread。 阅读本文以获取更详细的说明http://www.ibm.com/developerworks/java/library/j-jtp05236/