2 * Copyright (c) 2013, 2017 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.manager.testingservices.threadpool;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import com.google.common.collect.Lists;
14 import java.io.Closeable;
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ThreadPoolExecutor;
23 public class TestingFixedThreadPool implements TestingThreadPoolIfc, Closeable,
24 TestingModifiableThreadPoolIfc {
25 private final ThreadPoolExecutor executorService;
26 private final String uniqueName;
28 public static void cleanUp() {
29 for (ExecutorService executorService : ALL_EXECUTORS) {
30 executorService.shutdown();
32 ALL_EXECUTORS.clear();
35 // for verification purposes:
36 public static final List<ThreadPoolExecutor> ALL_EXECUTORS = Collections
37 .synchronizedList(Lists.<ThreadPoolExecutor>newLinkedList());
39 public TestingFixedThreadPool(final int threadCount, final String uniqueName) {
40 checkNotNull(uniqueName);
41 this.uniqueName = uniqueName;
42 executorService = (ThreadPoolExecutor) Executors
43 .newFixedThreadPool(threadCount);
44 ALL_EXECUTORS.add(executorService);
48 public Executor getExecutor() {
49 return executorService;
53 public void close() throws IOException {
54 executorService.shutdown();
55 ALL_EXECUTORS.remove(executorService);
60 public int getMaxNumberOfThreads() {
61 return executorService.getMaximumPoolSize();
64 public String getUniqueName() {
69 public void setMaximumNumberOfThreads(final int activeCount) {
70 checkArgument(activeCount > 0);
71 executorService.setMaximumPoolSize(activeCount);