]> git.basschouten.com Git - openhab-addons.git/blob
aabf3f39477d2b0a66902a3fc1a46affec944846
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.cbus.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.ThreadPoolExecutor;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.common.ThreadPoolManager;
22
23 import com.daveoxley.cbus.CGateThreadPool;
24 import com.daveoxley.cbus.CGateThreadPoolExecutor;
25
26 /**
27  * The {@link CBusThreadPool} is responsible for executing jobs from a threadpool
28  *
29  * @author John Harvey - Initial contribution
30  */
31
32 @NonNullByDefault
33 public class CBusThreadPool extends CGateThreadPool {
34
35     private final Map<String, @Nullable CGateThreadPoolExecutor> executorMap = new HashMap<>();
36
37     @Override
38     protected synchronized CGateThreadPoolExecutor CreateExecutor(@Nullable String name) {
39         if (name == null || name.isEmpty()) {
40             name = "_default";
41         }
42         @Nullable
43         CGateThreadPoolExecutor executor = executorMap.get(name);
44         if (executor != null) {
45             return executor;
46         }
47         CGateThreadPoolExecutor newExecutor = new CBusThreadPoolExecutor(name);
48         executorMap.put(name, newExecutor);
49         return newExecutor;
50     }
51
52     @NonNullByDefault
53     public class CBusThreadPoolExecutor extends CGateThreadPoolExecutor {
54         private final ThreadPoolExecutor threadPool;
55
56         public CBusThreadPoolExecutor(@Nullable String poolName) {
57             threadPool = (ThreadPoolExecutor) ThreadPoolManager.getPool("binding.cbus-" + poolName);
58         }
59
60         @Override
61         protected void execute(@Nullable Runnable runnable) {
62             if (runnable != null) {
63                 threadPool.execute(runnable);
64             }
65         }
66     }
67 }