e3ee6b2d380c7434cdda79a7248fa9e59aeaea02
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ExecutorServiceUtil.java
1 /*
2  * Copyright (c) 2014 Cisco 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 package org.opendaylight.yangtools.util;
9
10 import com.google.common.util.concurrent.ForwardingBlockingQueue;
11 import java.util.concurrent.BlockingQueue;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.RejectedExecutionException;
14 import java.util.concurrent.RejectedExecutionHandler;
15 import java.util.concurrent.ThreadPoolExecutor;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nonnull;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Utility methods for dealing with {@link ExecutorService}s.
23  */
24 public final class ExecutorServiceUtil {
25     private static final class WaitInQueueExecutionHandler implements RejectedExecutionHandler {
26         @Override
27         public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
28             if (executor.isShutdown() ) {
29                 throw new RejectedExecutionException( "Executor has been shutdown." );
30             }
31
32             try {
33                 executor.getQueue().put(r);
34             } catch (InterruptedException e) {
35                 LOG.debug("Interrupted while attempting to put to the queue", e);
36                 throw new RejectedExecutionException("Interrupted while attempting to put to the queue", e);
37             }
38         }
39     }
40
41     private static final Logger LOG = LoggerFactory.getLogger(ExecutorServiceUtil.class);
42     private static final RejectedExecutionHandler WAIT_IN_QUEUE_HANDLER = new WaitInQueueExecutionHandler();
43
44     private ExecutorServiceUtil() {
45         throw new UnsupportedOperationException("Utility class");
46     }
47
48     /**
49      * Creates a {@link BlockingQueue} which does not allow for non-blocking addition to the queue.
50      * This is useful with {@link #waitInQueueExecutionHandler()} to turn force a
51      * {@link ThreadPoolExecutor} to create as many threads as it is configured to before starting
52      * to fill the queue.
53      *
54      * @param delegate Backing blocking queue.
55      * @return A new blocking queue backed by the delegate
56      */
57     public static <E> BlockingQueue<E> offerFailingBlockingQueue(final BlockingQueue<E> delegate) {
58         return new ForwardingBlockingQueue<E>() {
59             @Override
60             public boolean offer(@Nonnull final E o) {
61                 return false;
62             }
63
64             @Override
65             protected BlockingQueue<E> delegate() {
66                 return delegate;
67             }
68         };
69     }
70
71     /**
72      * Returns a {@link RejectedExecutionHandler} which blocks on the {@link ThreadPoolExecutor}'s
73      * backing queue if a new thread cannot be spawned.
74      *
75      * @return A shared RejectedExecutionHandler instance.
76      */
77     public static RejectedExecutionHandler waitInQueueExecutionHandler() {
78         return WAIT_IN_QUEUE_HANDLER;
79     }
80
81     /**
82      * Tries to shutdown the given executor gracefully by awaiting termination for the given
83      * timeout period. If the timeout elapses before termination, the executor is forcefully
84      * shutdown.
85      */
86     public static void tryGracefulShutdown(final ExecutorService executor, long timeout,
87             TimeUnit unit ) {
88
89         executor.shutdown();
90
91         try {
92             if (!executor.awaitTermination(timeout, unit)) {
93                 executor.shutdownNow();
94             }
95         } catch (InterruptedException e) {
96             executor.shutdownNow();
97         }
98     }
99 }