blob: 0659198548a58516a5a3b7c6aada38016f1bf04b [file] [log] [blame]
Jian Li66f15262016-03-03 11:18:40 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
39 private static SharedScheduledExecutorService singleThreadExecutor =
40 new SharedScheduledExecutorService(
41 newSingleThreadScheduledExecutor(
42 groupedThreads("onos/shared/scheduled",
43 "onos-single-executor")));
44
45 private static SharedScheduledExecutorService poolThreadExecutor =
46 new SharedScheduledExecutorService(
47 newScheduledThreadPool(DEFAULT_POOL_SIZE,
48 groupedThreads("onos/shared/scheduled",
49 "onos-pool-executor-%d")));
50
51 // Ban public construction
52 private SharedScheduledExecutors() {
53 }
54
55 /**
56 * Returns the shared scheduled single thread executor.
57 *
58 * @return shared scheduled single thread executor
59 */
Jian Lia1d7f272016-03-28 17:21:47 -070060 public static SharedScheduledExecutorService getSingleThreadExecutor() {
Jian Li66f15262016-03-03 11:18:40 -080061 return singleThreadExecutor;
62 }
63
64 /**
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070065 * Executes one-shot timer task on shared thread pool.
66 *
67 * @param task timer task to execute
68 * @param delay before executing the task
69 * @param unit of delay
70 * @return a ScheduledFuture representing pending completion of the task
71 * and whose get() method will return null upon completion
72 */
73 public static ScheduledFuture<?> newTimeout(Runnable task, long delay, TimeUnit unit) {
74 return SharedScheduledExecutors.getPoolThreadExecutor()
75 .schedule(task, delay, unit);
76 }
77
78 /**
Jian Li66f15262016-03-03 11:18:40 -080079 * Returns the shared scheduled thread pool executor.
80 *
81 * @return shared scheduled executor pool
82 */
Jian Lia1d7f272016-03-28 17:21:47 -070083 public static SharedScheduledExecutorService getPoolThreadExecutor() {
Jian Li66f15262016-03-03 11:18:40 -080084 return poolThreadExecutor;
85 }
86
87 /**
88 * Configures the shared scheduled thread pool size.
89 *
90 * @param poolSize new pool size
91 */
92 public static void setPoolSize(int poolSize) {
93 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
94 poolThreadExecutor.setBackingExecutor(
95 newScheduledThreadPool(poolSize, groupedThreads("onos/shared/scheduled",
96 "onos-pool-executor-%d")));
97 }
98
99 /**
100 * Shuts down all shared scheduled executors.
101 * This is not intended to be called by application directly.
102 */
103 public static void shutdown() {
104 singleThreadExecutor.backingExecutor().shutdown();
105 poolThreadExecutor.backingExecutor().shutdown();
106 }
107}