Merge "Improved sorting of augmentations before code generation."
[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
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.util.concurrent.ForwardingBlockingQueue;
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             try {
29                 executor.getQueue().put(r);
30             } catch (InterruptedException e) {
31                 LOG.debug("Intterupted while waiting for queue", e);
32                 throw new RejectedExecutionException("Interrupted while waiting for queue", e);
33             }
34         }
35     }
36
37     private static final Logger LOG = LoggerFactory.getLogger(ExecutorServiceUtil.class);
38     private static final RejectedExecutionHandler WAIT_IN_QUEUE_HANDLER = new WaitInQueueExecutionHandler();
39
40     private ExecutorServiceUtil() {
41         throw new UnsupportedOperationException("Utility class");
42     }
43
44     /**
45      * Create a {@link BlockingQueue} which does not allow for non-blocking addition to the queue.
46      * This is useful with {@link #waitInQueueExecutionHandler()} to turn force a
47      * {@link ThreadPoolExecutor} to create as many threads as it is configured to before starting
48      * to fill the queue.
49      *
50      * @param delegate Backing blocking queue.
51      * @return A new blocking queue backed by the delegate
52      */
53     public <E> BlockingQueue<E> offerFailingBlockingQueue(final BlockingQueue<E> delegate) {
54         return new ForwardingBlockingQueue<E>() {
55             @Override
56             public boolean offer(final E o) {
57                 return false;
58             }
59
60             @Override
61             protected BlockingQueue<E> delegate() {
62                 return delegate;
63             }
64         };
65     }
66
67     /**
68      * Return a {@link RejectedExecutionHandler} which blocks on the {@link ThreadPoolExecutor}'s
69      * backing queue if a new thread cannot be spawned.
70      *
71      * @return A shared RejectedExecutionHandler instance.
72      */
73     public RejectedExecutionHandler waitInQueueExecutionHandler() {
74         return WAIT_IN_QUEUE_HANDLER;
75     }
76 }