blob: 626aafb04842d2ccde08d1230a3911c388ddc72e [file] [log] [blame]
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.util;
18
Thomas Vachuska945e7b02015-03-18 14:23:53 -070019import java.util.Timer;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070020import java.util.concurrent.ExecutorService;
Murat Parlakisik553db172015-04-08 03:29:04 -070021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070023
Thomas Vachuska945e7b02015-03-18 14:23:53 -070024import static java.util.concurrent.Executors.newFixedThreadPool;
25import static java.util.concurrent.Executors.newSingleThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070026import static org.onlab.util.Tools.groupedThreads;
27
28/**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070029 * Utility for managing a set of shared execution resources, such as a timer,
30 * single thread executor and thread pool executor for use by various parts of
31 * the platform or by applications.
32 * <p>
33 * Whenever possible, use of these shared resources is encouraged over creating
34 * separate ones.
35 * </p>
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070036 */
37public final class SharedExecutors {
38
Murat Parlakisik553db172015-04-08 03:29:04 -070039 private static final Logger log = LoggerFactory.getLogger(SharedExecutors.class);
40
Thomas Vachuska945e7b02015-03-18 14:23:53 -070041 // TODO: make this configurable via setPoolSize static method
Murat Parlakisik553db172015-04-08 03:29:04 -070042 public static final int DEFAULT_THREAD_SIZE = 30;
43 private static int poolSize = DEFAULT_THREAD_SIZE;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070044
45 private static ExecutorService singleThreadExecutor =
Thomas Vachuska945e7b02015-03-18 14:23:53 -070046 newSingleThreadExecutor(groupedThreads("onos/shared",
47 "onos-single-executor"));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070048
49 private static ExecutorService poolThreadExecutor =
Murat Parlakisik553db172015-04-08 03:29:04 -070050 newFixedThreadPool(poolSize, groupedThreads("onos/shared",
Thomas Vachuska945e7b02015-03-18 14:23:53 -070051 "onos-pool-executor-%d"));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070052
Thomas Vachuska945e7b02015-03-18 14:23:53 -070053 private static Timer sharedTimer = new Timer("onos-shared-timer");
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070054
Thomas Vachuska945e7b02015-03-18 14:23:53 -070055 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070056 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070057 }
58
59 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070060 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070061 *
62 * @return shared single thread executor
63 */
64 public static ExecutorService getSingleThreadExecutor() {
65 return singleThreadExecutor;
66 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070067
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070068 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070069 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070070 *
71 * @return shared executor pool
72 */
73 public static ExecutorService getPoolThreadExecutor() {
74 return poolThreadExecutor;
75 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070076
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070077 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070078 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070079 *
80 * @return shared timer
81 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070082 public static Timer getTimer() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070083 return sharedTimer;
84 }
85
Murat Parlakisik553db172015-04-08 03:29:04 -070086 /**
87 * Sets the shared thread pool size.
88 * @param poolSize
89 */
90 public static void setPoolSize(int poolSize) {
91 if (poolSize > 0) {
92 SharedExecutors.poolSize = poolSize;
93 //TODO: wait for execution previous task in the queue .
94 poolThreadExecutor.shutdown();
95 poolThreadExecutor = newFixedThreadPool(poolSize, groupedThreads("onos/shared",
96 "onos-pool-executor-%d"));
97 } else {
98 log.warn("Shared Pool Size size must be greater than 0");
99 }
100 }
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700101}