我在java文档中看到了这一点:ScheduledAtFixedRate,它说
如果执行了任务 遇到异常,随后 执行被压制
我不希望在我的应用程序中发生这种情况。即使我看到异常,我也总是希望后续执行发生并继续。如何从ScheduledExecutorService
获得此行为。
答案 0 :(得分:7)
使用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) )而没有别的。)
答案 1 :(得分:2)
从VerboseRunnable
尝试jcabi-log课程,该课程执行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 :(得分:1)
我遇到了同样的问题。我也试过run()方法中的try块但它不起作用。
所以到目前为止我做了一些工作:
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将停止工作。
答案 3 :(得分:1)
如果你想要的只是后续执行并且即使在异常之后继续,这个代码也应该有效。
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
,您可能不希望后续执行被执行。
这是输出
Fri Nov 23 12:09:38 JST 2012 print
_WARN ...任务将会 即使在例外情况已经提出11月23日星期五之后,也会继续 12:09:41 JST 2012印刷
_WARN ...任务将继续进行 即使在例外情况发生后,也就是2012年12月23日12:09:44 印刷
_WARN ...任务即使经过一段时间也会继续 异常已经提出了星期五11月23日12:09:47 JST 2012印刷
即使在异常提出后,_WARN ...任务仍将继续执行