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 | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 22 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 23 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 24 | import static org.onlab.util.Tools.groupedThreads; |
| 25 | |
| 26 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 27 | * Utility for managing a set of shared execution resources, such as a timer, |
| 28 | * single thread executor and thread pool executor for use by various parts of |
| 29 | * the platform or by applications. |
| 30 | * <p> |
| 31 | * Whenever possible, use of these shared resources is encouraged over creating |
| 32 | * separate ones. |
| 33 | * </p> |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 34 | */ |
| 35 | public final class SharedExecutors { |
| 36 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 37 | // TODO: make this configurable via setPoolSize static method |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 38 | private static int numberOfThreads = 30; |
| 39 | |
| 40 | private static ExecutorService singleThreadExecutor = |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 41 | newSingleThreadExecutor(groupedThreads("onos/shared", |
| 42 | "onos-single-executor")); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 43 | |
| 44 | private static ExecutorService poolThreadExecutor = |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 45 | newFixedThreadPool(numberOfThreads, groupedThreads("onos/shared", |
| 46 | "onos-pool-executor-%d")); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 47 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 48 | private static Timer sharedTimer = new Timer("onos-shared-timer"); |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 49 | |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 50 | // Ban public construction |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 51 | private SharedExecutors() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 55 | * Returns the shared single thread executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 56 | * |
| 57 | * @return shared single thread executor |
| 58 | */ |
| 59 | public static ExecutorService getSingleThreadExecutor() { |
| 60 | return singleThreadExecutor; |
| 61 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 62 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 63 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 64 | * Returns the shared thread pool executor. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 65 | * |
| 66 | * @return shared executor pool |
| 67 | */ |
| 68 | public static ExecutorService getPoolThreadExecutor() { |
| 69 | return poolThreadExecutor; |
| 70 | } |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 71 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 72 | /** |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 73 | * Returns the shared timer. |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 74 | * |
| 75 | * @return shared timer |
| 76 | */ |
Thomas Vachuska | 945e7b0 | 2015-03-18 14:23:53 -0700 | [diff] [blame] | 77 | public static Timer getTimer() { |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 78 | return sharedTimer; |
| 79 | } |
| 80 | |
Murat Parlakisik | af1042d | 2015-03-14 01:08:29 -0700 | [diff] [blame] | 81 | } |