发布
社区首页 >问答首页 >Java -用“返回”关闭线程不起作用

Java -用“返回”关闭线程不起作用
EN

Stack Overflow用户
提问于 2015-05-15 10:28:53
回答 3查看 1K关注 0票数 0

在包含我的main方法的类中有以下代码

代码语言:javascript
代码运行次数:0
复制
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类的实例。代码如下(为了这个示例,我已经把我的所有功能都去掉了)。

代码语言:javascript
代码运行次数:0
复制
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(){}方法中杀死线程,我会非常感激它(最好不要设置一个变量,我将在主类中签入它,但如果我必须这样做,我知道,一旦它到达返回语句,它就完成了,并且不再需要对在run()中创建的变量的任何引用。

向控制台生成的输出如下:

代码语言:javascript
代码运行次数:0
复制
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.
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-15 10:36:47

代码语言:javascript
代码运行次数:0
复制
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;

另一种更简单的方法是:

代码语言:javascript
代码运行次数:0
复制
   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();
    }

Executor awaitTermination方法替代了您的循环。

票数 0
EN

Stack Overflow用户

发布于 2015-05-15 10:32:27

你从不关机executor。来自isTerminated的Javadocs

如果关闭后所有任务都已完成,则返回true。注意,除非首先调用了isTerminated或shutdownNow,否则shutdownNow永远都不是真的。

只要不关闭executor,就可以向其提交新任务以供执行。isTerminated不检查提交任务的状态,而是检查ExecutorService本身的状态。

票数 2
EN

Stack Overflow用户

发布于 2015-05-15 11:01:06

ExecutorService主要用于将你的工作委托给分离工人。使用此操作,只创建了大多数连接池库。因此,无论何时启动应用程序,并通过应用程序的关闭方法(手动)关闭该服务,都将启动该服务。

在您的程序中,您应该编写一个代码来检查executor服务的状态,

有没有工人正在工作..。如果没有工人处于忙碌状态,那么就停止接受任何进一步的任务。

从javadocs中可以看到示例关闭代码,

以下方法分两个阶段关闭ExecutorService,首先通过调用关机来拒绝传入任务,然后在必要时调用shutdownNow以取消任何遗留的任务:

代码语言:javascript
代码运行次数:0
复制
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();
   }
 }

希望它能给你一些信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30257218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档