2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
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.eclipse.jdt.annotation.NonNull;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Utility methods for dealing with {@link ExecutorService}s.
24 public final class ExecutorServiceUtil {
25 private static final class WaitInQueueExecutionHandler implements RejectedExecutionHandler {
27 @SuppressWarnings("checkstyle:parameterName")
28 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
29 if (executor.isShutdown()) {
30 throw new RejectedExecutionException("Executor has been shutdown.");
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);
42 private static final Logger LOG = LoggerFactory.getLogger(ExecutorServiceUtil.class);
43 private static final @NonNull RejectedExecutionHandler WAIT_IN_QUEUE_HANDLER = new WaitInQueueExecutionHandler();
45 private ExecutorServiceUtil() {
50 * Creates a {@link BlockingQueue} which does not allow for non-blocking addition to the queue. This is useful with
51 * {@link #waitInQueueExecutionHandler()} to turn force a {@link ThreadPoolExecutor} to create as many threads as it
52 * is configured to before starting to fill the queue.
54 * @param <E> type of elements
55 * @param delegate Backing blocking queue.
56 * @return A new blocking queue backed by the delegate
58 public static <E> @NonNull BlockingQueue<E> offerFailingBlockingQueue(final BlockingQueue<E> delegate) {
59 return new ForwardingBlockingQueue<>() {
61 @SuppressWarnings("checkstyle:parameterName")
62 public boolean offer(final E o) {
67 protected BlockingQueue<E> delegate() {
74 * Returns a {@link RejectedExecutionHandler} which blocks on the {@link ThreadPoolExecutor}'s backing queue if a
75 * new thread cannot be spawned.
77 * @return A shared RejectedExecutionHandler instance.
79 public static @NonNull RejectedExecutionHandler waitInQueueExecutionHandler() {
80 return WAIT_IN_QUEUE_HANDLER;
84 * Tries to shutdown the given executor gracefully by awaiting termination for the given timeout period. If the
85 * timeout elapses before termination, the executor is forcefully shutdown.
87 * @param executor Executor to shut down
88 * @param timeout timeout period
89 * @param unit timeout unit
91 public static void tryGracefulShutdown(final @NonNull ExecutorService executor, final long timeout,
92 final @NonNull TimeUnit unit) {
96 if (!executor.awaitTermination(timeout, unit)) {
97 executor.shutdownNow();
99 } catch (InterruptedException e) {
100 executor.shutdownNow();