Merge branch 'master' of ../controller
[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 static java.util.Objects.requireNonNull;
12
13 import java.util.concurrent.RejectedExecutionHandler;
14 import java.util.concurrent.ThreadPoolExecutor;
15 import java.util.concurrent.atomic.LongAdder;
16 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
17
18 /**
19  * A RejectedExecutionHandler that delegates to a backing RejectedExecutionHandler and counts the
20  * number of rejected tasks.
21  *
22  * @author Thomas Pantelis
23  */
24 public class CountingRejectedExecutionHandler implements RejectedExecutionHandler {
25     private final LongAdder rejectedTaskCounter = new LongAdder();
26     private final RejectedExecutionHandler delegate;
27
28     /**
29      * Constructor.
30      *
31      * @param delegate the backing RejectedExecutionHandler.
32      */
33     public CountingRejectedExecutionHandler(final RejectedExecutionHandler delegate) {
34         this.delegate = requireNonNull(delegate);
35     }
36
37     @Override
38     public void rejectedExecution(final Runnable task, final ThreadPoolExecutor executor) {
39         rejectedTaskCounter.increment();
40         delegate.rejectedExecution(task, executor);
41     }
42
43     /**
44      * Returns the rejected task count.
45      */
46     public long getRejectedTaskCount() {
47         return rejectedTaskCounter.sum();
48     }
49
50     /**
51      * Returns a counting handler for rejected tasks that runs the rejected task directly in the
52      * calling thread of the execute method, unless the executor has been shut down, in which case
53      * the task is discarded.
54      */
55     public static CountingRejectedExecutionHandler newCallerRunsPolicy() {
56         return new CountingRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
57     }
58
59     /**
60      * Returns a counting handler for rejected tasks that throws a RejectedExecutionException.
61      */
62     public static CountingRejectedExecutionHandler newAbortPolicy() {
63         return new CountingRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
64     }
65
66     /**
67      * Returns a counting handler for rejected tasks that blocks on the
68      * {@link ThreadPoolExecutor}'s backing queue until it can add the task to the queue.
69      */
70     public static CountingRejectedExecutionHandler newCallerWaitsPolicy() {
71         return new CountingRejectedExecutionHandler(ExecutorServiceUtil.waitInQueueExecutionHandler());
72     }
73 }