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