Merge "Added DELETE support for Bridge and Port resources"
[controller.git] / opendaylight / config / threadpool-config-impl / src / main / java / org / opendaylight / controller / config / threadpool / util / FixedThreadPoolWrapper.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.config.threadpool.util;
10
11 import java.io.Closeable;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.ThreadFactory;
15 import java.util.concurrent.ThreadPoolExecutor;
16
17 import org.opendaylight.controller.config.threadpool.ThreadPool;
18
19 /**
20  * Implementation of {@link ThreadPool} using fixed number of threads wraps
21  * {@link ExecutorService}.
22  */
23 public class FixedThreadPoolWrapper implements ThreadPool, Closeable {
24
25     private final ThreadPoolExecutor executor;
26
27     public FixedThreadPoolWrapper(int threadCount, ThreadFactory factory) {
28         this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount, factory);
29         executor.prestartAllCoreThreads();
30     }
31
32     @Override
33     public ExecutorService getExecutor() {
34         return Executors.unconfigurableExecutorService(executor);
35     }
36
37     @Override
38     public void close() {
39         executor.shutdown();
40     }
41
42     @Override
43     public int getMaxThreadCount() {
44         return executor.getMaximumPoolSize();
45     }
46
47     public void setMaxThreadCount(int maxThreadCount) {
48         executor.setCorePoolSize(maxThreadCount);
49         executor.setMaximumPoolSize(maxThreadCount);
50     }
51 }