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