Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 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 | package org.onlab.util; |
| 17 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 20 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 21 | import static org.onlab.util.Tools.groupedThreads; |
| 22 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ScheduledFuture; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 26 | /** |
| 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 | */ |
| 35 | public 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 Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 60 | public static SharedScheduledExecutorService getSingleThreadExecutor() { |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 61 | return singleThreadExecutor; |
| 62 | } |
| 63 | |
| 64 | /** |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 65 | * 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 Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 79 | * Returns the shared scheduled thread pool executor. |
| 80 | * |
| 81 | * @return shared scheduled executor pool |
| 82 | */ |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 83 | public static SharedScheduledExecutorService getPoolThreadExecutor() { |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 84 | 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 | } |