blob: eca59bd7b11b92ef302f1166a990413bbb7ce2ef [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 Parlakisikaf1042d2015-03-14 01:08:29 -070021
Thomas Vachuska945e7b02015-03-18 14:23:53 -070022import static java.util.concurrent.Executors.newFixedThreadPool;
23import static java.util.concurrent.Executors.newSingleThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070024import static org.onlab.util.Tools.groupedThreads;
25
26/**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070027 * Utility for managing a set of shared execution resources, such as a timer,
28 * single thread executor and thread pool executor for use by various parts of
29 * the platform or by applications.
30 * <p>
31 * Whenever possible, use of these shared resources is encouraged over creating
32 * separate ones.
33 * </p>
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070034 */
35public final class SharedExecutors {
36
Thomas Vachuska945e7b02015-03-18 14:23:53 -070037 // TODO: make this configurable via setPoolSize static method
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070038 private static int numberOfThreads = 30;
39
40 private static ExecutorService singleThreadExecutor =
Thomas Vachuska945e7b02015-03-18 14:23:53 -070041 newSingleThreadExecutor(groupedThreads("onos/shared",
42 "onos-single-executor"));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070043
44 private static ExecutorService poolThreadExecutor =
Thomas Vachuska945e7b02015-03-18 14:23:53 -070045 newFixedThreadPool(numberOfThreads, groupedThreads("onos/shared",
46 "onos-pool-executor-%d"));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070047
Thomas Vachuska945e7b02015-03-18 14:23:53 -070048 private static Timer sharedTimer = new Timer("onos-shared-timer");
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070049
Thomas Vachuska945e7b02015-03-18 14:23:53 -070050 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070051 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070052 }
53
54 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070055 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070056 *
57 * @return shared single thread executor
58 */
59 public static ExecutorService getSingleThreadExecutor() {
60 return singleThreadExecutor;
61 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070062
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070063 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070064 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070065 *
66 * @return shared executor pool
67 */
68 public static ExecutorService getPoolThreadExecutor() {
69 return poolThreadExecutor;
70 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070071
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070072 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070073 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070074 *
75 * @return shared timer
76 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070077 public static Timer getTimer() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070078 return sharedTimer;
79 }
80
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070081}