]> git.basschouten.com Git - openhab-addons.git/blob
319c985d02c8e4328c02668d5f115aff7f973114
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ExecutorService;
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 @NonNullByDefault
32 public class CBusThreadPool extends CGateThreadPool {
33
34     private final Map<String, CGateThreadPoolExecutor> executorMap = new HashMap<>();
35
36     @Override
37     protected synchronized CGateThreadPoolExecutor CreateExecutor(@Nullable String name) {
38         String nullSafeName = name == null || name.isEmpty() ? "_default" : name;
39         CGateThreadPoolExecutor executor = executorMap.get(nullSafeName);
40         if (executor == null) {
41             executor = new CBusThreadPoolExecutor(nullSafeName);
42             executorMap.put(nullSafeName, executor);
43         }
44         return executor;
45     }
46
47     public class CBusThreadPoolExecutor extends CGateThreadPoolExecutor {
48         private final ExecutorService threadPool;
49
50         public CBusThreadPoolExecutor(@Nullable String poolName) {
51             threadPool = ThreadPoolManager.getPool("binding.cbus-" + poolName);
52         }
53
54         @Override
55         protected void execute(@Nullable Runnable runnable) {
56             if (runnable != null) {
57                 threadPool.execute(runnable);
58             }
59         }
60     }
61 }