【发布时间】:2015-10-22 22:49:58
【问题描述】:
我有一个类将启动一个带有无限循环的 Runnable。在某些时候,我想关闭程序,并且我想以一种干净的方式来完成它(例如,确保在执行结束时线程会将一些缓冲区刷新到输出文件等)。我该怎么做?
注意:在 cmets 中我写了 // Version 1 和 // Version 2。那是因为我尝试了两个版本,但都没有工作,我不断收到 InterruptedException。
主类:
public class MainClass {
private static Thread myThread;
private static MyRunnable myRunnable;
private static Logger logger;
public static void main( String[] args ) {
...
myRunnable = new MyRunnable(logger);
myThread = new Thread(myRunnable);
myThread.start();
...
}
private static void shutDown() {
logger.debug("Shut down everything.");
if(myRunnable != null){
myRunnable.shutdown();
try {
myThread.join();
logger.debug("Runnable Stopped");
} catch (InterruptedException e) { // Always failing here
e.printStackTrace();
}
}
// Now stop logger and other things
logger.debug("Shutdown other stuff");
...
logger.debug("Shutdown logger");
Configurator.shutdown((LoggerContext)LogManager.getContext());
System.out.println("Done");
}
}
可运行类:
public class MyRunnable implements Runnable {
private AtomicBoolean stop = new AtomicBoolean(false); // Version 1
public boolean isStopped() {
return stop.get(); // Version 1
return Thread.currentThread().isInterrupted(); // Version 2
}
public void shutdown() {
logger.debug("Stopping my runnable");
stop.set(true); // Version 1
Thread.currentThread().interrupt(); // Version 2
}
@Override
public void run() {
try{
while(!isStopped()) {
// Do things
}
logger.debug("Exiting from infinite loop");
} catch(Exception e){
logger.error("Exception! " + e.getMessage());
} finally {
logger.debug("Entering in finally part");
// Flush buffer, commit to database, ... close stuff
}
}
}
但是shutDown函数的输出总是一样的:
Shut down everything.
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1260)
at java.lang.Thread.join(Thread.java:1334)
at com.my.packageName.MainClass.shutDown(MainClass.java:374)
Shutdown other stuff
Shutdown logger
Done
如果我删除 join() 函数,异常就会消失,但输出仍然相同。
编辑:我刚刚注意到,如果我评论 myRunnable.shutdown(); 行并继续使用 myThread.join(); 行,即使我没有停止该线程,我仍然会遇到异常。这是为什么?是因为 Tread 和 Runnable 是两个不同的东西吗?如果是这样,那么我该如何停止 Runnable 并等待其完成?
【问题讨论】:
标签: java multithreading runnable