2 * Copyright (c) 2013 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.controller.config.threadpool.util;
10 import java.io.Closeable;
11 import java.util.OptionalInt;
12 import java.util.concurrent.BlockingQueue;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.RejectedExecutionException;
17 import java.util.concurrent.RejectedExecutionHandler;
18 import java.util.concurrent.ThreadFactory;
19 import java.util.concurrent.ThreadPoolExecutor;
20 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.controller.config.threadpool.ThreadPool;
24 * Implementation of {@link ThreadPool} using flexible number of threads wraps
25 * {@link ExecutorService}.
27 public class FlexibleThreadPoolWrapper implements ThreadPool, Closeable {
28 private final ThreadPoolExecutor executor;
30 public FlexibleThreadPoolWrapper(final int minThreadCount, final int maxThreadCount, final long keepAlive,
31 final TimeUnit timeUnit, final ThreadFactory threadFactory) {
32 this(minThreadCount, maxThreadCount, keepAlive, timeUnit, threadFactory, getQueue(OptionalInt.empty()));
35 public FlexibleThreadPoolWrapper(final int minThreadCount, final int maxThreadCount, final long keepAlive,
36 final TimeUnit timeUnit, final ThreadFactory threadFactory, final OptionalInt queueCapacity) {
37 this(minThreadCount, maxThreadCount, keepAlive, timeUnit, threadFactory, getQueue(queueCapacity));
40 private FlexibleThreadPoolWrapper(final int minThreadCount, final int maxThreadCount, final long keepAlive,
41 final TimeUnit timeUnit, final ThreadFactory threadFactory, final BlockingQueue<Runnable> queue) {
43 executor = new ThreadPoolExecutor(minThreadCount, maxThreadCount, keepAlive, timeUnit,
44 queue, threadFactory, new FlexibleRejectionHandler());
45 executor.prestartAllCoreThreads();
49 * Overriding the queue:
50 * ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
51 * occurs in RejectedExecutionHandler.
52 * This impl saturates threadpool first, then queue. When both are full caller will get blocked.
54 private static ForwardingBlockingQueue getQueue(final OptionalInt capacity) {
55 return new ForwardingBlockingQueue(
56 capacity.isPresent() ? new LinkedBlockingQueue<>(capacity.orElseThrow()) : new LinkedBlockingQueue<>());
60 public ExecutorService getExecutor() {
61 return Executors.unconfigurableExecutorService(executor);
64 public int getMinThreadCount() {
65 return executor.getCorePoolSize();
68 public void setMinThreadCount(final int minThreadCount) {
69 executor.setCorePoolSize(minThreadCount);
73 public int getMaxThreadCount() {
74 return executor.getMaximumPoolSize();
77 public void setMaxThreadCount(final int maxThreadCount) {
78 executor.setMaximumPoolSize(maxThreadCount);
81 public long getKeepAliveMillis() {
82 return executor.getKeepAliveTime(TimeUnit.MILLISECONDS);
85 public void setKeepAliveMillis(final long keepAliveMillis) {
86 executor.setKeepAliveTime(keepAliveMillis, TimeUnit.MILLISECONDS);
89 public void setThreadFactory(final ThreadFactory threadFactory) {
90 executor.setThreadFactory(threadFactory);
93 public void prestartAllCoreThreads() {
94 executor.prestartAllCoreThreads();
103 * if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
105 private static final class FlexibleRejectionHandler implements RejectedExecutionHandler {
107 @SuppressWarnings("checkstyle:parameterName")
108 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
110 executor.getQueue().put(r);
111 } catch (InterruptedException e) {
112 throw new RejectedExecutionException("Interrupted while waiting on the queue", e);
117 private static final class ForwardingBlockingQueue
118 extends com.google.common.util.concurrent.ForwardingBlockingQueue<Runnable> {
119 private final BlockingQueue<Runnable> delegate;
121 ForwardingBlockingQueue(final BlockingQueue<Runnable> delegate) {
122 this.delegate = delegate;
126 protected BlockingQueue<Runnable> delegate() {
131 @SuppressWarnings("checkstyle:parameterName")
132 public boolean offer(final Runnable o) {
133 // ThreadPoolExecutor will spawn a new thread after core size is reached only
134 // if the queue.offer returns false.