【发布时间】:2012-08-01 08:36:06
【问题描述】:
这是我关于尝试使用中断结束/退出线程和处理 Ctrl-c 结束的第二篇文章。我不确定我是否理解它,但这是我最好的尝试。我需要更清楚的概念,请尽可能提供代码示例。
有两个类,主类Quitit和另一个类thething。主班。
通过终端加载程序时(Linux上的情况):
Java -jar Quitit.jar
当你 Ctrl-c 关闭它时,我说你需要这样做是正确的:
Runtime.getRuntime().addShutdownHook()
您的线程,以便它们在关闭时被杀死。
- 这是正确的处理方法吗?
- 为什么它不允许您调用方法以便它可以正常关闭?
当不通过 Ctrl-C 关闭并且您希望通过 Thread.Interrupt() 关闭时,下面的程序是否正确使用它?
- 我是否正确地说 Thread.Join() 会暂停调用线程,直到目标线程在继续之前停止?
- 您将如何在 implements Runnable 线程上实现/调用相同的 Runtime.getRuntime().addShutdownHook(),而不是 extends Thread?
退出课程:
public class Quitit extends Thread {
public Quitit(String name) {
try {
connect("sdfsd");
} catch (Exception ex) {
}
}
void connect(String portName) throws Exception {
Thread thh = new thething("blaghname");
Runtime.getRuntime().addShutdownHook(thh);
thh.start();
System.out.println("Thread Thh (thething) Started()");
while (true) {
try {
Thread.sleep(2000);
if (thh.isAlive()) {
System.out.println("Thread Thh (thething) isAlive");
if (thh.isInterrupted()) {
System.out.println("Thread Thh (thething) is Inturrupted Should be Shutting Down");
} else {
System.out.println("Thread Thh (thething) is Not Inturrupted");
thh.interrupt();
System.out.println("Thread Thh (thething) Inturrput Sent");
System.out.println("Thread Thh (thething) Joined()");
thh.join();
}
} else {
System.out.println("Thread Thh (thething) isDead");
System.out.println("Main Thread:: can now end After Sleep off 2 seconds");
Thread.sleep(2000);
System.out.println("MMain Thread:: Sleep Ended Calling Break");
break;
}
} catch (InterruptedException xa) {
System.out.println("Main Thread:: ending due to InterruptException second Break called");
break;
}
}
System.out.println("Main Thread:: Outside While(true) via Break call");
}
public static void main(String[] args) {
try {
Thread oop = new Quitit("");
Runtime.getRuntime().addShutdownHook(oop);
oop.start();
} catch (Exception ezx) {
System.out.println("Main Thread:: Not Expected Exception");
}
}
}
TheThing 类:
public class thething extends Thread {
thething(String name) {
super(name);
}
@Override
public void run() {
while (true) {
try {
System.out.println("thething class:: Inside while(true) Loop, now sleeping for 2 seconds");
Thread.sleep(2000);
} catch (InterruptedException e) {
try {
System.out.println("thething class:: has been Inturrupted now sleeping for 2 seconds!!");
Thread.sleep(2000);
break; // Will Quit the While(true) Loop
} catch (InterruptedException ex) {
System.out.println("thething class:: Second InterruptedException called !!");
}
}
}
System.out.println("thething class:: Outside while(true) and now thread is dying");
}
}
输出::
run:
Thread Thh (thething) Started()
thething class:: Inside while(true) Loop, now sleeping for 2 seconds
Thread Thh (thething) isAlive
Thread Thh (thething) is Not Inturrupted
Thread Thh (thething) Inturrput Sent
Thread Thh (thething) Joined()
thething class:: has been Inturrupted now sleeping for 2 seconds!!
thething class:: Outside while(true) and now thread is dying
Thread Thh (thething) isDead
Main Thread:: can now end After Sleep off 2 seconds
MMain Thread:: Sleep Ended Calling Break
Main Thread:: Outside While(true) via Break call
FINISHED - BUILD SUCCESSFUL (total time: 8 seconds)
【问题讨论】:
-
你需要在
Quitit构造函数中调用super() -
好吧,这又让人困惑了,所以 interrupt() 不是杀死线程的正确方法。您应该使用一个布尔标志来终止线程,该标志将控制 while(true)/while(false) 循环并让线程耗尽。仅当您具有诸如串行端口读取()(IO读取)之类的保持状态时才应使用中断,在这种情况下,您可以在没有超时的情况下进行读取(),从而可以无限期地保持程序?似乎真的缺少清晰度还是只有我?
-
@DevilCode:中断是请求线程停止的正确方法。不需要任何额外的标志:中断状态可以做到这一点。
-
JB Nizet:这与其他 cmets 背道而驰。
-
是的。我在对 Andrzej 的回答的评论中解释了我的立场
标签: java multithreading runtime