Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | |
| 17 | package org.onlab.util; |
| 18 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 19 | import java.util.Timer; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 20 | import java.util.concurrent.ExecutorService; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 21 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 23 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 24 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 25 | import static org.onlab.util.Tools.groupedThreads; |
| 26 | |
| 27 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 28 | * Utility for managing a set of shared execution resources, such as a timer, |
| 29 | * single thread executor and thread pool executor for use by various parts of |
| 30 | * the platform or by applications. |
| 31 | * <p> |
| 32 | * Whenever possible, use of these shared resources is encouraged over creating |
| 33 | * separate ones. |
| 34 | * </p> |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 35 | */ |
| 36 | public final class SharedExecutors { |
| 37 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 38 | public static final int DEFAULT_POOL_SIZE = 30; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 39 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 40 | private static SharedExecutorService singleThreadExecutor = |
| 41 | new SharedExecutorService( |
| 42 | newSingleThreadExecutor(groupedThreads("onos/shared", |
| 43 | "onos-single-executor"))); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 44 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 45 | private static SharedExecutorService poolThreadExecutor = |
| 46 | new SharedExecutorService( |
| 47 | newFixedThreadPool(DEFAULT_POOL_SIZE, |
| 48 | groupedThreads("onos/shared", |
| 49 | "onos-pool-executor-%d"))); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 50 | |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 51 | private static SharedTimer sharedTimer = new SharedTimer(); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 52 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 53 | // Ban public construction |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 54 | private SharedExecutors() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 58 | * Returns the shared single thread executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 59 | * |
| 60 | * @return shared single thread executor |
| 61 | */ |
| 62 | public static ExecutorService getSingleThreadExecutor() { |
| 63 | return singleThreadExecutor; |
| 64 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 65 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 66 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 67 | * Returns the shared thread pool executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 68 | * |
| 69 | * @return shared executor pool |
| 70 | */ |
| 71 | public static ExecutorService getPoolThreadExecutor() { |
| 72 | return poolThreadExecutor; |
| 73 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 74 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 75 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 76 | * Returns the shared timer. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 77 | * |
| 78 | * @return shared timer |
| 79 | */ |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 80 | public static Timer getTimer() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 81 | return sharedTimer; |
| 82 | } |
| 83 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Sets the shared thread pool size. |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 86 | * |
| 87 | * @param poolSize new pool size |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 88 | */ |
| 89 | public static void setPoolSize(int poolSize) { |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 90 | checkArgument(poolSize > 0, "Shared pool size size must be greater than 0"); |
| 91 | poolThreadExecutor.setBackingExecutor( |
| 92 | newFixedThreadPool(poolSize, groupedThreads("onos/shared", |
| 93 | "onos-pool-executor-%d"))); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Shuts down all shared timers and executors and therefore should be |
| 98 | * called only by the framework. |
| 99 | */ |
| 100 | public static void shutdown() { |
| 101 | sharedTimer.shutdown(); |
| 102 | singleThreadExecutor.backingExecutor().shutdown(); |
| 103 | poolThreadExecutor.backingExecutor().shutdown(); |
| 104 | } |
| 105 | |
| 106 | // Timer extension which does not allow outside cancel method. |
| 107 | private static class SharedTimer extends Timer { |
| 108 | |
| 109 | public SharedTimer() { |
| 110 | super("onos-shared-timer"); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void cancel() { |
| 115 | throw new UnsupportedOperationException("Cancel of shared timer is not allowed"); |
| 116 | } |
| 117 | |
| 118 | private void shutdown() { |
| 119 | super.cancel(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 122 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 123 | } |