问题 如何使用ScheduledExecutorService重新安排任务?


我在java文档中看到了这个: ScheduledAtFixedRate, 它说

如果执行了任务   遇到异常,随后   执行被压制

我不希望在我的应用程序中发生这种情况。即使我看到异常,我也总是希望后续执行发生并继续。我怎样才能从中获得这种行为 ScheduledExecutorService


1560
2018-03-12 07:02


起源

我更喜欢中描述的解决方案 CosmoCode博客 - Chobicus
解决方案 CosmoCode博客  块 (使用 future.get();),这是由提供的异步执行点 Executor秒。 - user454322


答案:


使用try / catch包围Callable.call方法或Runnable.run方法...

例如:

public void run()
{
    try
    {
        // ... code
    }
    catch(final IOException ex)
    {
        // handle it
    }
    catch(final RuntimeException ex)
    {
        // handle it
    }
    catch(final Exception ex)
    {
        // handle it
    }
    catch(final Error ex)
    {
        // handle it
    }
    catch(final Throwable ex)
    {
        // handle it
    }
}

注意除了编译器告诉你的东西之外还有其它东西(我的样本中的IOException)不是一个好主意,但有时候,这听起来像其中之一,如果你正确处理它可以解决它。

请记住像Error这样的东西非常糟糕 - 虚拟机内存不足等等......所以要小心你如何处理它们(这就是为什么我把它们分成自己的处理程序而不仅仅是捕获(最终的Throwable ex)而没有什么其他)。


7
2018-03-12 07:14



请注意,如果您没有在重复的计划任务中捕获throwable并且确实发生了OOME,那么您将永远不会发现它(除非在ScheduledFuture上调用get()并记录ExecutionExceotions - oxbow_lakes
那么...猜你必须... ick :-)我会验证然后更新我的答案 - thx - TofuBeer
是否因特定线程终止而停止后续执行?如果线程池大小大于1,这大部分都是正确的,那么为什么ScheduledExecutorService实现不能尝试使用不同的线程运行任务? - RRM


尝试 VerboseRunnable 来自 jcabi日志,这是由TofuBeer建议的包装:

import com.jcabi.log.VerboseRunnable;
Runnable runnable = new VerboseRunnable(
  Runnable() {
    public void run() { 
      // do business logic, may Exception occurs
    }
  },
  true // it means that all exceptions will be swallowed and logged
);

现在,当有人打电话时 runnable.run() 没有异常被抛出。相反,他们被吞下并记录(到SLF4J)。


2
2018-05-10 15:51



尼斯, Exceptions被吞下了 VerboseRunnable,因此后续任务将被执行。 - user454322


我有同样的问题。我也尝试在run()方法中尝试阻止,但它不起作用。

所以到目前为止我做了一些工作:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class Test2 {

    static final ExecutorService pool = Executors.newFixedThreadPool(3);

    static final R1 r1 = new R1();
    static final R2 r2 = new R2();

    static final BlockingQueue deadRunnablesQueue = new LinkedBlockingQueue<IdentifiableRunnable>();

    static final Runnable supervisor = new Supervisor(pool, deadRunnablesQueue);

    public static void main(String[] args) {
        pool.submit(r1);
        pool.submit(r2);
        new Thread(supervisor).start();
    }

    static void reSubmit(IdentifiableRunnable r) {
        System.out.println("given to an error, runnable [" + r.getId()
                + "] will be resubmited");
        deadRunnablesQueue.add(r);
    }

    static interface IdentifiableRunnable extends Runnable {
        String getId();
    }

    static class Supervisor implements Runnable {
        private final ExecutorService pool;
        private final BlockingQueue<IdentifiableRunnable> deadRunnablesQueue;

        Supervisor(final ExecutorService pool,
                final BlockingQueue<IdentifiableRunnable> deadRunnablesQueue) {
            this.pool = pool;
            this.deadRunnablesQueue = deadRunnablesQueue;
        }

        @Override
        public void run() {
            while (true) {
                IdentifiableRunnable r = null;
                System.out.println("");
                System.out
                        .println("Supervisor will wait for a new runnable in order to resubmit it...");
                try {
                    System.out.println();
                    r = deadRunnablesQueue.take();
                } catch (InterruptedException e) {
                }
                if (r != null) {
                    System.out.println("Supervisor got runnable [" + r.getId()
                            + "] to resubmit ");
                    pool.submit(r);
                }
            }
        }
    }

    static class R1 implements IdentifiableRunnable {
        private final String id = "R1";
        private long l;

        @Override
        public void run() {
            while (true) {
                System.out.println("R1 " + (l++));
                try {
                    Thread.currentThread().sleep(5000);
                } catch (InterruptedException e) {
                    System.err.println("R1 InterruptedException:");
                }
            }
        }

        public String getId() {
            return id;
        }
    }

    static class R2 implements IdentifiableRunnable {
        private final String id = "R2";
        private long l;

        @Override
        public void run() {
            try {
                while (true) {
                    System.out.println("R2 " + (l++));
                    try {
                        Thread.currentThread().sleep(5000);
                    } catch (InterruptedException e) {
                        System.err.println("R2 InterruptedException:");
                    }
                    if (l == 3) {
                        throw new RuntimeException(
                                "R2 error.. Should I continue to process ? ");
                    }
                }
            } catch (final Throwable t) {
                t.printStackTrace();
                Test2.reSubmit(this);
            }
        }

        public String getId() {
            return id;
        }
    }

}

您可以尝试注释掉Test2.reSubmit(this)以查看没有它,R2将停止工作。


1
2017-10-06 00:23



澄清:实际上ScheduledExecutorService可以在run()方法中使用try块。上面的示例基于ExecutorService。 - Rodolfo
感谢您分享您的代码。这是一个很好的例子,看起来很复杂。 - user454322


如果你想要的只是 随后的执行将发生并继续 即使在例外之后,这段代码也应该有用。

 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();    

 Runnable task = new Runnable() {      
  @Override
  public void run() {
   try{
      System.out.println(new Date() + " printing");
      if(true)
        throw new RuntimeException();

   } catch (Exception exc) {
      System.out.println(" WARN...task will continiue"+ 
            "running even after an Exception has araised");
    }
  }      
};

executor.scheduleAtFixedRate(task, 0, 3, TimeUnit.SECONDS);

如果一个 Throwable 以外 Exception 已发生您可能不希望后续执行被执行。

这是输出

周五11月23日12:09:38 JST 2012印刷
 _WARN ...任务会   即使在异常提出后也会继续
11月23日星期五   12:09:41 JST 2012印刷
 _WARN ......任务将继续   即使在异常提出后也是如此
星期五11月23日12:09:44 JST 2012   印花
 _WARN ...任务即使经过一段时间也会继续   异常提出了
星期五11月23日12:09:47 JST 2012印刷
  _WARN ...即使在异常提出后,任务也将继续执行


1
2017-10-05 03:18