Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 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 | */ |
| 32 | public 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 Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 57 | public static SharedScheduledExecutorService getSingleThreadExecutor() { |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 58 | return singleThreadExecutor; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the shared scheduled thread pool executor. |
| 63 | * |
| 64 | * @return shared scheduled executor pool |
| 65 | */ |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 66 | public static SharedScheduledExecutorService getPoolThreadExecutor() { |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 67 | 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 | } |