X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fconfig%2Fthreadpool-config-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fthreadpool%2Futil%2FFlexibleThreadPoolWrapper.java;fp=opendaylight%2Fconfig%2Fthreadpool-config-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fthreadpool%2Futil%2FFlexibleThreadPoolWrapper.java;h=3dfa6e2bc756419b18f3224b32b127255e00d35d;hb=353b76bddbe7a939a4a8ed54a3b794e6866660d7;hp=0000000000000000000000000000000000000000;hpb=e87fa215eac79bc76b0addbb1f5beebfaa8ca5f7;p=controller.git diff --git a/opendaylight/config/threadpool-config-impl/src/main/java/org/opendaylight/controller/config/threadpool/util/FlexibleThreadPoolWrapper.java b/opendaylight/config/threadpool-config-impl/src/main/java/org/opendaylight/controller/config/threadpool/util/FlexibleThreadPoolWrapper.java new file mode 100644 index 0000000000..3dfa6e2bc7 --- /dev/null +++ b/opendaylight/config/threadpool-config-impl/src/main/java/org/opendaylight/controller/config/threadpool/util/FlexibleThreadPoolWrapper.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.config.threadpool.util; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.opendaylight.controller.config.threadpool.ThreadPool; + +/** + * Implementation of {@link ThreadPool} using flexible number of threads wraps + * {@link ExecutorService}. + */ +public class FlexibleThreadPoolWrapper implements ThreadPool, Closeable { + private final ThreadPoolExecutor executor; + + public FlexibleThreadPoolWrapper(int minThreadCount, int maxThreadCount, long keepAlive, TimeUnit timeUnit, + ThreadFactory threadFactory) { + + executor = new ThreadPoolExecutor(minThreadCount, maxThreadCount, keepAlive, timeUnit, + new SynchronousQueue(), threadFactory); + executor.prestartAllCoreThreads(); + } + + @Override + public ExecutorService getExecutor() { + return Executors.unconfigurableExecutorService(executor); + } + + public int getMinThreadCount() { + return executor.getCorePoolSize(); + } + + public void setMinThreadCount(int minThreadCount) { + executor.setCorePoolSize(minThreadCount); + } + + @Override + public int getMaxThreadCount() { + return executor.getMaximumPoolSize(); + } + + public void setMaxThreadCount(int maxThreadCount) { + executor.setMaximumPoolSize(maxThreadCount); + } + + public long getKeepAliveMillis() { + return executor.getKeepAliveTime(TimeUnit.MILLISECONDS); + } + + public void setKeepAliveMillis(long keepAliveMillis) { + executor.setKeepAliveTime(keepAliveMillis, TimeUnit.MILLISECONDS); + } + + public void setThreadFactory(ThreadFactory threadFactory) { + executor.setThreadFactory(threadFactory); + } + + public void prestartAllCoreThreads() { + executor.prestartAllCoreThreads(); + } + + @Override + public void close() throws IOException { + executor.shutdown(); + } + +}