blob: 0dadce85dac89a46b8608e8517605f3daf95043e [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 Vachuskab0317c62015-04-08 23:58:58 -070022import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuska945e7b02015-03-18 14:23:53 -070023import static java.util.concurrent.Executors.newFixedThreadPool;
24import static java.util.concurrent.Executors.newSingleThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070025import static org.onlab.util.Tools.groupedThreads;
26
27/**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070028 * Utility for managing a set of shared execution resources, such as a timer,
29 * single thread executor and thread pool executor for use by various parts of
30 * the platform or by applications.
31 * <p>
32 * Whenever possible, use of these shared resources is encouraged over creating
33 * separate ones.
34 * </p>
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070035 */
36public final class SharedExecutors {
37
Thomas Vachuskab0317c62015-04-08 23:58:58 -070038 public static final int DEFAULT_POOL_SIZE = 30;
Murat Parlakisik553db172015-04-08 03:29:04 -070039
Thomas Vachuskab0317c62015-04-08 23:58:58 -070040 private static SharedExecutorService singleThreadExecutor =
41 new SharedExecutorService(
42 newSingleThreadExecutor(groupedThreads("onos/shared",
43 "onos-single-executor")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070044
Thomas Vachuskab0317c62015-04-08 23:58:58 -070045 private static SharedExecutorService poolThreadExecutor =
46 new SharedExecutorService(
47 newFixedThreadPool(DEFAULT_POOL_SIZE,
48 groupedThreads("onos/shared",
49 "onos-pool-executor-%d")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070050
Thomas Vachuskab0317c62015-04-08 23:58:58 -070051 private static SharedTimer sharedTimer = new SharedTimer();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070052
Thomas Vachuska945e7b02015-03-18 14:23:53 -070053 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070054 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070055 }
56
57 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070058 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070059 *
60 * @return shared single thread executor
61 */
62 public static ExecutorService getSingleThreadExecutor() {
63 return singleThreadExecutor;
64 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070065
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070066 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070067 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070068 *
69 * @return shared executor pool
70 */
71 public static ExecutorService getPoolThreadExecutor() {
72 return poolThreadExecutor;
73 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070074
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070075 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070076 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070077 *
78 * @return shared timer
79 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070080 public static Timer getTimer() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070081 return sharedTimer;
82 }
83
Murat Parlakisik553db172015-04-08 03:29:04 -070084 /**
85 * Sets the shared thread pool size.
Thomas Vachuskab0317c62015-04-08 23:58:58 -070086 *
87 * @param poolSize new pool size
Murat Parlakisik553db172015-04-08 03:29:04 -070088 */
89 public static void setPoolSize(int poolSize) {
Thomas Vachuskab0317c62015-04-08 23:58:58 -070090 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
91 poolThreadExecutor.setBackingExecutor(
92 newFixedThreadPool(poolSize, groupedThreads("onos/shared",
93 "onos-pool-executor-%d")));
94 }
95
96 /**
97 * Shuts down all shared timers and executors and therefore should be
98 * called only by the framework.
99 */
100 public static void shutdown() {
101 sharedTimer.shutdown();
102 singleThreadExecutor.backingExecutor().shutdown();
103 poolThreadExecutor.backingExecutor().shutdown();
104 }
105
106 // Timer extension which does not allow outside cancel method.
107 private static class SharedTimer extends Timer {
108
109 public SharedTimer() {
110 super("onos-shared-timer");
111 }
112
113 @Override
114 public void cancel() {
115 throw new UnsupportedOperationException("Cancel of shared timer is not allowed");
116 }
117
118 private void shutdown() {
119 super.cancel();
Murat Parlakisik553db172015-04-08 03:29:04 -0700120 }
121 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700122
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700123}