Organize Imports to be Checkstyle compliant in utils
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / CountingRejectedExecutionHandler.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.RejectedExecutionHandler;
13 import java.util.concurrent.ThreadPoolExecutor;
14 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
15 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
16
17 /**
18  * A RejectedExecutionHandler that delegates to a backing RejectedExecutionHandler and counts the
19  * number of rejected tasks.
20  *
21  * @author Thomas Pantelis
22  */
23 public class CountingRejectedExecutionHandler implements RejectedExecutionHandler {
24     private static final AtomicLongFieldUpdater<CountingRejectedExecutionHandler> COUNTER_UPDATER =
25             AtomicLongFieldUpdater.newUpdater(CountingRejectedExecutionHandler.class, "rejectedTaskCounter");
26     private final RejectedExecutionHandler delegate;
27     private volatile long rejectedTaskCounter;
28
29     /**
30      * Constructor.
31      *
32      * @param delegate the backing RejectedExecutionHandler.
33      */
34     public CountingRejectedExecutionHandler( final RejectedExecutionHandler delegate ) {
35         this.delegate = Preconditions.checkNotNull( delegate );
36     }
37
38     @Override
39     public void rejectedExecution( final Runnable task, final ThreadPoolExecutor executor ) {
40         COUNTER_UPDATER.incrementAndGet(this);
41         delegate.rejectedExecution( task, executor );
42     }
43
44     /**
45      * Returns the rejected task count.
46      */
47     public long getRejectedTaskCount() {
48         return rejectedTaskCounter;
49     }
50
51     /**
52      * Returns s counting handler for rejected tasks that runs the rejected task directly in the
53      * calling thread of the execute method, unless the executor has been shut down, in which case
54      * the task is discarded.
55      */
56     public static CountingRejectedExecutionHandler newCallerRunsPolicy() {
57         return new CountingRejectedExecutionHandler( new ThreadPoolExecutor.CallerRunsPolicy() );
58     }
59
60     /**
61      * Returns a counting handler for rejected tasks that throws a RejectedExecutionException.
62      */
63     public static CountingRejectedExecutionHandler newAbortPolicy() {
64         return new CountingRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy() );
65     }
66
67     /**
68      * Returns a counting handler for rejected tasks that that blocks on the
69      * {@link ThreadPoolExecutor}'s backing queue until it can add the task to the queue.
70      */
71     public static CountingRejectedExecutionHandler newCallerWaitsPolicy() {
72         return new CountingRejectedExecutionHandler( ExecutorServiceUtil.waitInQueueExecutionHandler() );
73     }
74 }