本文整理了Java中java.util.concurrent.ThreadPoolExecutor.afterExecute()
方法的一些代码示例,展示了ThreadPoolExecutor.afterExecute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadPoolExecutor.afterExecute()
方法的具体详情如下:
包路径:java.util.concurrent.ThreadPoolExecutor
类名称:ThreadPoolExecutor
方法名:afterExecute
[英]Method invoked upon completion of execution of the given Runnable. This method is invoked by the thread that executed the task. If non-null, the Throwable is the uncaught RuntimeExceptionor Error that caused execution to terminate abruptly.
This implementation does nothing, but may be customized in subclasses. Note: To properly nest multiple overridings, subclasses should generally invoke super.afterExecute at the beginning of this method.
Note: When actions are enclosed in tasks (such as FutureTask) either explicitly or via methods such as submit, these task objects catch and maintain computational exceptions, and so they do not cause abrupt termination, and the internal exceptions are not passed to this method. If you would like to trap both kinds of failures in this method, you can further probe for such cases, as in this sample subclass that prints either the direct cause or the underlying exception if a task has been aborted:
class ExtendedExecutor extends ThreadPoolExecutor catch (CancellationException ce)
t = ce;
} catch (ExecutionException ee)
t = ee.getCause();
} catch (InterruptedException ie)
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
System.out.println(t);
}
}}
[中]方法在给定Runnable的执行完成时调用。此方法由执行任务的线程调用。如果非null,则Throwable是导致执行突然终止的未捕获运行时异常或错误。
这个实现什么都不做,但可以在子类中进行定制。注意:为了正确嵌套多个重写,子类通常应该调用super。afterExecute在这个方法的开头。
注意:当操作显式或通过submit等方法包含在任务(如FutureTask)中时,这些任务对象捕获并维护计算异常,因此它们不会导致突然终止,并且内部异常不会传递给此方法。如果您想在此方法中捕获这两种类型的故障,可以进一步探测此类情况,例如在这个示例子类中,如果任务已中止,则打印直接原因或底层异常:
class ExtendedExecutor extends ThreadPoolExecutor catch (CancellationException ce)
t = ce;
} catch (ExecutionException ee)
t = ee.getCause();
} catch (InterruptedException ie)
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
System.out.println(t);
}
}}
代码示例来源:origin: apache/hbase
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
running.remove(Thread.currentThread());
}
代码示例来源:origin: Netflix/Priam
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
active.decrementAndGet();
}
代码示例来源:origin: objectbox/objectbox-java
@Override
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
boxStore.closeThreadResources();
}
代码示例来源:origin: apache/hbase
@Override
protected void afterExecute(Runnable r, Throwable t) {
metrics.decActiveWorkerCount();
super.afterExecute(r, t);
}
}
代码示例来源:origin: apache/hive
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Future<?> future = (Future<?>) r;
if (future.isDone()) {
future.get();
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
} catch (Throwable t2) {
t = t2;
}
}
if (t instanceof OutOfMemoryError) {
oomHook.run();
}
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
ExecutorHelper.logThrowableFromAfterExecute(r, t);
}
}
代码示例来源:origin: apache/activemq
@Override
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
if (runnable instanceof StoreTask) {
((StoreTask)runnable).releaseLocks();
}
}
}
代码示例来源:origin: apache/storm
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
// If future got interrupted exception, we want to interrupt parent thread itself.
Thread.currentThread().interrupt();
}
}
if (t != null) {
Utils.handleUncaughtException(t);
}
}
}
代码示例来源:origin: lealone/Lealone
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
logExceptionsAfterExecute(r, t);
}
代码示例来源:origin: jMonkeyEngine/jmonkeyengine
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Future<?> future = (Future<?>) r;
if (future.isDone())
future.get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
t.printStackTrace();
}
};
代码示例来源:origin: alibaba/jstorm
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
// If future got interrupted exception, we want to interrupt parent thread itself.
Thread.currentThread().interrupt();
}
}
if (t != null) {
Utils.handleUncaughtException(t);
}
}
}
代码示例来源:origin: azkaban/azkaban
@Override
protected void afterExecute(final Runnable r, final Throwable t) {
final long time = System.currentTimeMillis() - this.startTime.get().longValue();
synchronized (this) {
this.totalTime += time;
++this.totalTasks;
}
this.inProgress.remove(r);
super.afterExecute(r, t);
try {
this.executingListener.afterExecute(r);
} catch (final Throwable e) {
// to ensure the listener doesn't cause any issues
logger.warn("Listener threw exception", e);
}
}
代码示例来源:origin: apache/kylin
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
((Future<?>) r).get();
} catch (ExecutionException ee) {
logger.error("Execution exception when running task in " + Thread.currentThread().getName());
t = ee.getCause();
} catch (InterruptedException ie) {
logger.error("Thread interrupted: ");
Thread.currentThread().interrupt(); // ignore/reset
} catch (Throwable throwable) {
t = throwable;
}
}
if (t != null) {
logger.error("Caught exception in thread " + Thread.currentThread().getName() + ": ", t);
}
}
}
代码示例来源:origin: stagemonitor/stagemonitor
/**
* Overriding this method makes sure that exceptions thrown by a task are not silently swallowed.
* <p>
* Thanks to nos for this solution: http://stackoverflow.com/a/2248203/1125055
*/
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Future<?> future = (Future<?>) r;
if (future.isDone()) {
future.get();
}
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null) {
logger.warn("Error while executing task in " + this, t);
}
}
}
代码示例来源:origin: apache/hbase
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
stealFromCountDown.countDown();
}
};
代码示例来源:origin: 4thline/cling
@Override
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
if (throwable != null) {
Throwable cause = Exceptions.unwrap(throwable);
if (cause instanceof InterruptedException) {
// Ignore this, might happen when we shutdownNow() the executor. We can't
// log at this point as the logging system might be stopped already (e.g.
// if it's a CDI component).
return;
}
// Log only
log.warning("Thread terminated " + runnable + " abruptly with exception: " + throwable);
log.warning("Root cause: " + cause);
}
}
}
代码示例来源:origin: apache/hbase
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
stealJobCountDown.countDown();
}
代码示例来源:origin: jeasonlzy/okhttp-OkGo
/** 任务结束后回调 */
@Override
protected void afterExecute(final Runnable r, Throwable t) {
super.afterExecute(r, t);
if (taskEndListenerList != null && taskEndListenerList.size() > 0) {
for (final OnTaskEndListener listener : taskEndListenerList) {
innerHandler.post(new Runnable() {
@Override
public void run() {
listener.onTaskEnd(r);
}
});
}
}
//当前正在运行的数量为1 表示当前正在停止的任务,同时队列中没有任务,表示所有任务下载完毕
if (getActiveCount() == 1 && getQueue().size() == 0) {
if (allTaskEndListenerList != null && allTaskEndListenerList.size() > 0) {
for (final OnAllTaskEndListener listener : allTaskEndListenerList) {
innerHandler.post(new Runnable() {
@Override
public void run() {
listener.onAllTaskEnd();
}
});
}
}
}
}
代码示例来源:origin: kiegroup/jbpm
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
latch.countDown();
}
代码示例来源:origin: neo4j/neo4j
@Override
protected void afterExecute( Runnable r, Throwable t )
{
try
{
afterExecuteEvent.await();
super.afterExecute( r, t );
afterExecuteBarrier.countDown();
}
catch ( Throwable ex )
{
throw new RuntimeException( ex );
}
}
}
内容来源于网络,如有侵权,请联系作者删除!