blob: 9141438c8186cf8017b9efc3000a3f71fb9a60fd [file] [log] [blame]
Jian Li66f15262016-03-03 11:18:40 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li66f15262016-03-03 11:18:40 -08003 *
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
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070023import java.util.concurrent.ScheduledFuture;
24import java.util.concurrent.TimeUnit;
25
Jian Li66f15262016-03-03 11:18:40 -080026/**
27 * Utility for managing a set of shared execution resources, such as a single
28 * thread scheduled executor and thread pool scheduled executor for use by
29 * various parts of the platform or by applications.
30 * <p>
31 * Whenever possible, use of these shared resources is encouraged over creating
32 * separate ones.
33 * </p>
34 */
35public final class SharedScheduledExecutors {
36
37 public static final int DEFAULT_POOL_SIZE = 30;
38
Ray Milkey7fd9ae22019-04-02 09:34:23 -070039 private static final Object EXECUTORS_LOCK = new Object();
Ray Milkey397caca2019-04-01 16:27:50 -070040 private static SharedScheduledExecutorService singleThreadExecutor = null;
Ray Milkey397caca2019-04-01 16:27:50 -070041 private static SharedScheduledExecutorService poolThreadExecutor = null;
Jian Li66f15262016-03-03 11:18:40 -080042
43 // Ban public construction
44 private SharedScheduledExecutors() {
45 }
46
Ray Milkey397caca2019-04-01 16:27:50 -070047 private static void setup() {
Ray Milkey7fd9ae22019-04-02 09:34:23 -070048 synchronized (EXECUTORS_LOCK) {
49 if (singleThreadExecutor == null) {
50 singleThreadExecutor =
51 new SharedScheduledExecutorService(
52 newSingleThreadScheduledExecutor(
53 groupedThreads("onos/shared/scheduled",
54 "onos-single-executor")));
Ray Milkey397caca2019-04-01 16:27:50 -070055
Ray Milkey7fd9ae22019-04-02 09:34:23 -070056 poolThreadExecutor =
57 new SharedScheduledExecutorService(
58 newScheduledThreadPool(DEFAULT_POOL_SIZE,
59 groupedThreads("onos/shared/scheduled",
60 "onos-pool-executor-%d")));
61 }
Ray Milkey397caca2019-04-01 16:27:50 -070062 }
63 }
64
Jian Li66f15262016-03-03 11:18:40 -080065 /**
66 * Returns the shared scheduled single thread executor.
67 *
68 * @return shared scheduled single thread executor
69 */
Jian Lia1d7f272016-03-28 17:21:47 -070070 public static SharedScheduledExecutorService getSingleThreadExecutor() {
Ray Milkey397caca2019-04-01 16:27:50 -070071 setup();
Jian Li66f15262016-03-03 11:18:40 -080072 return singleThreadExecutor;
73 }
74
75 /**
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070076 * Executes one-shot timer task on shared thread pool.
77 *
78 * @param task timer task to execute
79 * @param delay before executing the task
80 * @param unit of delay
81 * @return a ScheduledFuture representing pending completion of the task
82 * and whose get() method will return null upon completion
83 */
84 public static ScheduledFuture<?> newTimeout(Runnable task, long delay, TimeUnit unit) {
Ray Milkey397caca2019-04-01 16:27:50 -070085 setup();
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070086 return SharedScheduledExecutors.getPoolThreadExecutor()
87 .schedule(task, delay, unit);
88 }
89
90 /**
Jian Li66f15262016-03-03 11:18:40 -080091 * Returns the shared scheduled thread pool executor.
92 *
93 * @return shared scheduled executor pool
94 */
Jian Lia1d7f272016-03-28 17:21:47 -070095 public static SharedScheduledExecutorService getPoolThreadExecutor() {
Ray Milkey397caca2019-04-01 16:27:50 -070096 setup();
Jian Li66f15262016-03-03 11:18:40 -080097 return poolThreadExecutor;
98 }
99
100 /**
101 * Configures the shared scheduled thread pool size.
102 *
103 * @param poolSize new pool size
104 */
105 public static void setPoolSize(int poolSize) {
Ray Milkey397caca2019-04-01 16:27:50 -0700106 setup();
Jian Li66f15262016-03-03 11:18:40 -0800107 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
108 poolThreadExecutor.setBackingExecutor(
109 newScheduledThreadPool(poolSize, groupedThreads("onos/shared/scheduled",
110 "onos-pool-executor-%d")));
111 }
112
113 /**
114 * Shuts down all shared scheduled executors.
115 * This is not intended to be called by application directly.
116 */
117 public static void shutdown() {
Ray Milkey7fd9ae22019-04-02 09:34:23 -0700118 synchronized (EXECUTORS_LOCK) {
Carmelo Cascone4d5607e2021-01-28 18:25:59 -0800119 if (singleThreadExecutor != null) {
120 singleThreadExecutor.backingExecutor().shutdown();
121 singleThreadExecutor = null;
122 poolThreadExecutor.backingExecutor().shutdown();
123 poolThreadExecutor = null;
124 }
Ray Milkey7fd9ae22019-04-02 09:34:23 -0700125 }
Jian Li66f15262016-03-03 11:18:40 -0800126 }
127}