为什么由ScheduledExecutorService.schedule()启动的线程从不终止? [英] Why does the thread started by ScheduledExecutorService.schedule() never terminate?

查看:1830
本文介绍了为什么由ScheduledExecutorService.schedule()启动的线程从不终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过调用ScheduledExecutorService.schedule()创建线程时,它不会在执行计划任务后终止。

When I create a thread by calling ScheduledExecutorService.schedule(), it never terminates after executing the scheduled task.

例如下面的程序从不退出: p>

For example the following program never quits:

public static void main(String[] args) {
  ScheduledFuture scheduledFuture = 
      Executors.newSingleThreadScheduledExecutor().schedule(new Callable() {

    public Void call() {
      doSomething();
      return null;
    }

  }, 1, TimeUnit.SECONDS);
}

public static void doSomething() {
}


$ b b

这是一个JDK错误,还是只是错过了一些东西?

Is this a JDK bug, or did I just miss something?

推荐答案

正在等待执行。

如果任务正在等待执行, future.cancel()将阻止它被执行cancel(true)/ cancel(false))。

If the task is waiting to be executed, future.cancel() will prevent it from being executed (both cancel(true)/cancel(false)).

如果任务已经执行, future.cancel(false)将不起作用。 future.cancel(true)将中断正在执行该任务的线程。 这是否会产生任何效果取决于您,谁将实施该任务。根据实现,任务可以响应或可以不响应中断。

If the task is already being executed, future.cancel(false) will have no effect. future.cancel(true) will interrupt the thread that is executing that task. Whether this will have any effect is up to you, who will implement that task. A task may or may not respond to interruption depending on the implementation.

为了使您的任务响应取消,您必须实现 doSomething()中断。

In order to make your task responsive to cancellation, you must implement doSomething() so that it will respond to interruption.

基本上有两种方法:

1.检查逻辑中的中断标志

1.Check interruption flag in your logic

public void doSomething(){
    stuff();
    //Don't use Thread.interrupt()
    if(Thread.currentThread().isInterrupted()){
        // We have an interruption request, possibly a cancel request
        //Stop doing what you are doing and terminate.
        return;
    }
    doLongRunningStuff();
}  

您必须偶尔检查中断标志,如果中断,正在做和返回。确保对检查使用Thread.isInterrupted()而不是Thread.interrupt()。

You must occasionally check for the interruption flag, and if interrupted, stop what you are doing and return. Be sure to use Thread.isInterrupted() and not Thread.interrupt() for the check.

2.Act on Interrupted exception

2.Act upon Interrupted exception

public void doSomething(){
    try{
        stuff();
    }catch(InterruptedException e){
        // We have an interruption request, possibly a cancel request

        // First, preserve Interrupted Status because InterruptedException clears the 
        // interrupted flag
        Thread.currentThread.interrupt();

        // Now stop doing your task and terminate
        return;
    }

    doLongRunningStuff();
}

当你有任何抛出InterruptedException的方法时,并在抛出时终止。

When you have any method that throws InterruptedException, be sure to stop what you doing and terminate when one is thrown.

以这种方式实现方法后,您可以调用future.cancel(true)取消正在运行的任务的执行。

Once you implement your methods in this way, you can call future.cancel(true) to cancel the execution of a running task.

这篇关于为什么由ScheduledExecutorService.schedule()启动的线程从不终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆