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.manager.testingservices.threadpool;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.io.Closeable;
14 import java.io.IOException;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.Executor;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ThreadPoolExecutor;
22 import com.google.common.collect.Lists;
24 public class TestingFixedThreadPool implements TestingThreadPoolIfc, Closeable,
25 TestingModifiableThreadPoolIfc {
26 private final ThreadPoolExecutor executorService;
27 private final String uniqueName;
29 public static void cleanUp() {
30 for (ExecutorService executorService : allExecutors) {
31 executorService.shutdown();
36 // for verification purposes:
37 public static final List<ThreadPoolExecutor> allExecutors = Collections
38 .synchronizedList(Lists.<ThreadPoolExecutor>newLinkedList());
40 public TestingFixedThreadPool(int threadCount, String uniqueName) {
41 checkNotNull(uniqueName);
42 this.uniqueName = uniqueName;
43 executorService = (ThreadPoolExecutor) Executors
44 .newFixedThreadPool(threadCount);
45 allExecutors.add(executorService);
49 public Executor getExecutor() {
50 return executorService;
54 public void close() throws IOException {
55 executorService.shutdown();
56 allExecutors.remove(executorService);
61 public int getMaxNumberOfThreads() {
62 return executorService.getMaximumPoolSize();
65 public String getUniqueName() {
70 public void setMaximumNumberOfThreads(int activeCount) {
71 checkArgument(activeCount > 0);
72 executorService.setMaximumPoolSize(activeCount);