package org.opendaylight.yangtools.util.concurrent;
+import com.google.common.base.Preconditions;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.atomic.AtomicLong;
-
+import java.util.concurrent.atomic.LongAdder;
import org.opendaylight.yangtools.util.ExecutorServiceUtil;
-import com.google.common.base.Preconditions;
-
/**
* A RejectedExecutionHandler that delegates to a backing RejectedExecutionHandler and counts the
* number of rejected tasks.
* @author Thomas Pantelis
*/
public class CountingRejectedExecutionHandler implements RejectedExecutionHandler {
-
+ private final LongAdder rejectedTaskCounter = new LongAdder();
private final RejectedExecutionHandler delegate;
- private final AtomicLong rejectedTaskCounter = new AtomicLong();
/**
* Constructor.
*
* @param delegate the backing RejectedExecutionHandler.
*/
- public CountingRejectedExecutionHandler( RejectedExecutionHandler delegate ) {
+ public CountingRejectedExecutionHandler( final RejectedExecutionHandler delegate ) {
this.delegate = Preconditions.checkNotNull( delegate );
}
@Override
- public void rejectedExecution( Runnable task, ThreadPoolExecutor executor ) {
- rejectedTaskCounter.incrementAndGet();
- delegate.rejectedExecution( task, executor );
+ public void rejectedExecution( final Runnable task, final ThreadPoolExecutor executor ) {
+ rejectedTaskCounter.increment();
+ delegate.rejectedExecution(task, executor);
}
/**
* Returns the rejected task count.
*/
- public long getRejectedTaskCount(){
- return rejectedTaskCounter.get();
+ public long getRejectedTaskCount() {
+ return rejectedTaskCounter.sum();
}
/**
* the task is discarded.
*/
public static CountingRejectedExecutionHandler newCallerRunsPolicy() {
- return new CountingRejectedExecutionHandler( new ThreadPoolExecutor.CallerRunsPolicy() );
+ return new CountingRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
}
/**
* Returns a counting handler for rejected tasks that throws a RejectedExecutionException.
*/
public static CountingRejectedExecutionHandler newAbortPolicy() {
- return new CountingRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy() );
+ return new CountingRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
}
/**
* {@link ThreadPoolExecutor}'s backing queue until it can add the task to the queue.
*/
public static CountingRejectedExecutionHandler newCallerWaitsPolicy() {
- return new CountingRejectedExecutionHandler( ExecutorServiceUtil.waitInQueueExecutionHandler() );
+ return new CountingRejectedExecutionHandler(ExecutorServiceUtil.waitInQueueExecutionHandler());
}
}