如何在Java中停止不间断线程
我有一个我无法编辑的Java应用程序,它启动了一个具有此run()
方法的java.lang.Thread
:
public void run(){ while(true){ System.out.println("Something"); } }
在某个时间点我想阻止它。 如果我使用Thread.interrupt()
它不起作用。 如果我使用Thread.stop()
它可以工作,但不推荐使用此方法(因此不鼓励使用它,因为它可能会在新版本中从JVM中删除)。
如何在Java中停止这种不间断的线程?
您可以使用check变量实现interrupt方法。
首先使用volatile检查变量:
volatile boolean tostop = false; // Keep the initial value to be false
接下来,定义要依赖于此变量的线程。
Thread thread1 = new Thread(){ public void run() { while(!tostop) { -- Write your code here -- } } }
接下来定义使用线程的函数:
public void ....{ //Interrupt code tostop = true; thread1.sleep(300); // Give the thread sometime for cleanup //Use System.exit(0), if the thread is in main function. }
希望这可以帮助。
java调试器允许您通过向其中注入exception来终止线程。
启动Java进程并让它侦听某个端口:
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=4444
并将调试器与以下内容连接:
jdb -attach 127.0.0.1:4444
并发出以下命令:
threads
获取正在运行的线程列表,并使用kill命令终止正在运行的线程。
kill 0xe2e new java.lang.IllegalArgumentException("er");
在此链接下: http : //download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html :
如果一个线程没有响应Thread.interrupt怎么办?
在某些情况下,您可以使用特定于应用程序的技巧。 例如,如果线程正在等待已知套接字,则可以关闭套接字以使线程立即返回。 不幸的是,确实没有任何技术可以发挥作用。 应该注意的是,在等待线程不响应Thread.interrupt的所有情况下,它也不会响应Thread.stop。 此类情况包括故意拒绝服务攻击,以及thread.stop和thread.interrupt无法正常工作的I / O操作。
没有来自该线程的合作,您无法可靠地中断线程。
作为一个丑陋的黑客(不是实际使用!),您可以替换System.out
并使println
在满足中断条件时抛出exception并且所讨论的线程是当前线程(即将其用作钩子以提供一些合作目标线程的上下文)。
编辑:更优雅的选项 – 当Thread.interrupted()
为true
,你可以通过抛出exception使println
中断,这样可以通过调用thread.interrupt()
来中断线程。
此方法的语义将非常接近普通的可中断方法(即抛出InterruptedException
方法),尽管您不能使用InterruptedException
因为它是一个已检查的exception,需要使用未经检查的RuntimeException
:
System.setOut(new PrintStream(System.out) { public void println(String s) { if (Thread.interrupted()) throw new RuntimeException(); super.println(s); } });
我不知道有任何已弃用的方法已经从JRE中删除了。 stop()已被弃用了很长时间,可能超过10年。 使用它是一个坏主意,但它可能是你最好的选择。
好的,我找到了解决方案,它真的很简单,只需睡眠线程然后中断它!
final Thread t = getTheThreadToClose(); //Final is important because only in this way you can pass it to the following new Thread if (t != null && t.isAlive() && !t.isInterrupted()){ try{ new Thread(){ public void run(){ try{ t.sleep(3000);//SLEEP INSIDE THE NEW THREAD } catch(InterruptedException ex){} } }.start(); t.interrupt();//INTERRUPT OUTSIDE THE NEW STARTED THREAD } catch(Exception e){} }
它适用于所有情况!
我想感谢你们所有人提供的非常有用的帮助,特别是venomrld和Toby用他们的话语给了我解决方案的灵感;-),这个奇妙的网站让我总能解决我所有的编程问题。
再次感谢大家和新年快乐!