我在java文档中看到了这个: ScheduledAtFixedRate, 它说
如果执行了任务 遇到异常,随后 执行被压制
我不希望在我的应用程序中发生这种情况。即使我看到异常,我也总是希望后续执行发生并继续。我怎样才能从中获得这种行为 ScheduledExecutorService
。
我在java文档中看到了这个: ScheduledAtFixedRate, 它说
如果执行了任务 遇到异常,随后 执行被压制
我不希望在我的应用程序中发生这种情况。即使我看到异常,我也总是希望后续执行发生并继续。我怎样才能从中获得这种行为 ScheduledExecutorService
。
使用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)而没有什么其他)。
尝试 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)。
我有同样的问题。我也尝试在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将停止工作。
如果你想要的只是 随后的执行将发生并继续 即使在例外之后,这段代码也应该有用。
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 ...即使在异常提出后,任务也将继续执行