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