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 | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame^] | 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 23 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 24 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 25 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 26 | import static org.onlab.util.Tools.groupedThreads; |
| 27 | |
| 28 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 29 | * Utility for managing a set of shared execution resources, such as a timer, |
| 30 | * single thread executor and thread pool executor for use by various parts of |
| 31 | * the platform or by applications. |
| 32 | * <p> |
| 33 | * Whenever possible, use of these shared resources is encouraged over creating |
| 34 | * separate ones. |
| 35 | * </p> |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 36 | */ |
| 37 | public final class SharedExecutors { |
| 38 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame^] | 39 | private static final Logger log = LoggerFactory.getLogger(SharedExecutors.class); |
| 40 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 41 | // TODO: make this configurable via setPoolSize static method |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame^] | 42 | public static final int DEFAULT_THREAD_SIZE = 30; |
| 43 | private static int poolSize = DEFAULT_THREAD_SIZE; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 44 | |
| 45 | private static ExecutorService singleThreadExecutor = |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 46 | newSingleThreadExecutor(groupedThreads("onos/shared", |
| 47 | "onos-single-executor")); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 48 | |
| 49 | private static ExecutorService poolThreadExecutor = |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame^] | 50 | newFixedThreadPool(poolSize, groupedThreads("onos/shared", |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 51 | "onos-pool-executor-%d")); |
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 | private static Timer sharedTimer = new Timer("onos-shared-timer"); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 55 | // Ban public construction |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 56 | private SharedExecutors() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 60 | * Returns the shared single thread executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 61 | * |
| 62 | * @return shared single thread executor |
| 63 | */ |
| 64 | public static ExecutorService getSingleThreadExecutor() { |
| 65 | return singleThreadExecutor; |
| 66 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 67 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 68 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 69 | * Returns the shared thread pool executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 70 | * |
| 71 | * @return shared executor pool |
| 72 | */ |
| 73 | public static ExecutorService getPoolThreadExecutor() { |
| 74 | return poolThreadExecutor; |
| 75 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 76 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 77 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 78 | * Returns the shared timer. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 79 | * |
| 80 | * @return shared timer |
| 81 | */ |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 82 | public static Timer getTimer() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 83 | return sharedTimer; |
| 84 | } |
| 85 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame^] | 86 | /** |
| 87 | * Sets the shared thread pool size. |
| 88 | * @param poolSize |
| 89 | */ |
| 90 | public static void setPoolSize(int poolSize) { |
| 91 | if (poolSize > 0) { |
| 92 | SharedExecutors.poolSize = poolSize; |
| 93 | //TODO: wait for execution previous task in the queue . |
| 94 | poolThreadExecutor.shutdown(); |
| 95 | poolThreadExecutor = newFixedThreadPool(poolSize, groupedThreads("onos/shared", |
| 96 | "onos-pool-executor-%d")); |
| 97 | } else { |
| 98 | log.warn("Shared Pool Size size must be greater than 0"); |
| 99 | } |
| 100 | } |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 101 | } |