[英]Java - Close a thread with 'return' not working
我在包含我的main
方法的类中有以下代码
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable formatConcentration = new formatConcentration(87);
executor.execute(formatConcentration);
System.out.println("Called an instance of formatConcentration");
while (!executor.isTerminated())
{
//stay Alive
Thread.sleep(1000);
System.out.println("Still alive");
}
System.out.println("Program successfully finished");
return;
这将创建一个formatConcentration
类的实例。 下面的代码(为示例起见,我将所有功能都删除了)。
public class formatConcentration extends Thread{
private int numberOfNewRows;
formatConcentration(int rows)
{
this.numberOfNewRows = rows;
}
public void run() {
System.out.println("Running the Formatting Script");
final int numberOfNewRegistered = this.numberOfNewRows;
try
{
System.out.println("Finished formatting");
}//end of try
catch (Exception e1)
{
log.log(e1.toString());
log.closeLog() ;
System.exit(1) ;
}
System.out.println("Still Finished formatting");
return;
}
}
我的问题是,一旦调用return
,它就不会终止线程。
我已经做了很多环顾四周,据我所知这应该没问题,但是我可以想象我正在忽略一些小东西,并且希望对此问题有新的了解。
或者,如果有人对如何从run(){}
方法内部杀死线程的建议,我将不胜感激(最好不要设置将在主类中检查的变量,但是如果这样做的话)我知道,一旦它到达return语句,它就完成了,不再需要引用在run()
创建的变量。
生成到控制台的输出如下:
Called an instance of formatConcentration
Running the Formatting Script
Finished formatting
Still Finished formatting
Still alive
Still alive
Still alive
Still alive
Still alive
etc.
您永远不会关闭executor
。 从Javadocs for isTerminated
:
如果所有任务在关闭后都已完成,则返回true。 请注意,除非先调用shutdown或shutdownNow,否则isTerminated永远不会为真。
只要不关闭executor
,就可以向其提交新任务以执行。 isTerminated
不检查的提交的任务的状态,它会检查状态ExecutorService
本身。
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable formatConcentration = new formatConcentration(87);
executor.execute(formatConcentration);
System.out.println("Called an instance of formatConcentration");
executor.shutdown();
while (!executor.isTerminated())
{
//stay Alive
Thread.sleep(1000);
System.out.println("Still alive");
}
System.out.println("Program successfully finished");
return;
另一个更简单的方法是:
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable formatConcentration = new formatConcentration(87);
executor.execute(formatConcentration);
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
执行程序的awaitTermination方法替代您的循环。
ExecutorService主要用于将您的工作委派给分工。 使用此功能,仅创建了大多数连接池库。 因此,只要您的应用程序启动,便会启动该服务,并通过应用程序的关闭方法将其关闭(手动)。
在您的程序中,您应该编写代码来检查执行程序服务的状态,
当前是否有任何工人在工作...如果没有工人处于忙碌模式,则停止接受任何其他任务。
从javadocs中查看示例关闭代码,
以下方法分两个阶段关闭ExecutorService:首先通过调用shutdown拒绝传入的任务,然后在必要时调用shutdownNow来取消所有遗留的任务:
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
希望它能给您一些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.