blob: 5469e62ab1fbdf7d99b79667dadea10f3b562a72 [file] [log] [blame]
Jian Li66f15262016-03-03 11:18:40 -08001/*
2 * Copyright 2016 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 */
16package org.onlab.util;
17
Jian Li66f15262016-03-03 11:18:40 -080018import static com.google.common.base.Preconditions.checkArgument;
19import static java.util.concurrent.Executors.newScheduledThreadPool;
20import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
21import static org.onlab.util.Tools.groupedThreads;
22
23/**
24 * Utility for managing a set of shared execution resources, such as a single
25 * thread scheduled executor and thread pool scheduled executor for use by
26 * various parts of the platform or by applications.
27 * <p>
28 * Whenever possible, use of these shared resources is encouraged over creating
29 * separate ones.
30 * </p>
31 */
32public final class SharedScheduledExecutors {
33
34 public static final int DEFAULT_POOL_SIZE = 30;
35
36 private static SharedScheduledExecutorService singleThreadExecutor =
37 new SharedScheduledExecutorService(
38 newSingleThreadScheduledExecutor(
39 groupedThreads("onos/shared/scheduled",
40 "onos-single-executor")));
41
42 private static SharedScheduledExecutorService poolThreadExecutor =
43 new SharedScheduledExecutorService(
44 newScheduledThreadPool(DEFAULT_POOL_SIZE,
45 groupedThreads("onos/shared/scheduled",
46 "onos-pool-executor-%d")));
47
48 // Ban public construction
49 private SharedScheduledExecutors() {
50 }
51
52 /**
53 * Returns the shared scheduled single thread executor.
54 *
55 * @return shared scheduled single thread executor
56 */
Jian Lia1d7f272016-03-28 17:21:47 -070057 public static SharedScheduledExecutorService getSingleThreadExecutor() {
Jian Li66f15262016-03-03 11:18:40 -080058 return singleThreadExecutor;
59 }
60
61 /**
62 * Returns the shared scheduled thread pool executor.
63 *
64 * @return shared scheduled executor pool
65 */
Jian Lia1d7f272016-03-28 17:21:47 -070066 public static SharedScheduledExecutorService getPoolThreadExecutor() {
Jian Li66f15262016-03-03 11:18:40 -080067 return poolThreadExecutor;
68 }
69
70 /**
71 * Configures the shared scheduled thread pool size.
72 *
73 * @param poolSize new pool size
74 */
75 public static void setPoolSize(int poolSize) {
76 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
77 poolThreadExecutor.setBackingExecutor(
78 newScheduledThreadPool(poolSize, groupedThreads("onos/shared/scheduled",
79 "onos-pool-executor-%d")));
80 }
81
82 /**
83 * Shuts down all shared scheduled executors.
84 * This is not intended to be called by application directly.
85 */
86 public static void shutdown() {
87 singleThreadExecutor.backingExecutor().shutdown();
88 poolThreadExecutor.backingExecutor().shutdown();
89 }
90}