[New] Added NamedThreadPoolFactory

This commit is contained in:
Robert von Burg 2019-02-06 14:33:45 +01:00
parent ed42640e42
commit 1e1ac785ea
2 changed files with 8 additions and 10 deletions

View File

@ -29,10 +29,10 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import li.strolch.agent.impl.ComponentContainerImpl; import li.strolch.agent.impl.ComponentContainerImpl;
import li.strolch.runtime.ThreadPoolFactory;
import li.strolch.runtime.configuration.ConfigurationParser; import li.strolch.runtime.configuration.ConfigurationParser;
import li.strolch.runtime.configuration.RuntimeConfiguration; import li.strolch.runtime.configuration.RuntimeConfiguration;
import li.strolch.runtime.configuration.StrolchConfiguration; import li.strolch.runtime.configuration.StrolchConfiguration;
import li.strolch.utils.NamedThreadPoolFactory;
import li.strolch.utils.dbc.DBC; import li.strolch.utils.dbc.DBC;
import li.strolch.utils.helper.StringHelper; import li.strolch.utils.helper.StringHelper;
import li.strolch.utils.helper.SystemHelper; import li.strolch.utils.helper.SystemHelper;
@ -97,7 +97,7 @@ public class StrolchAgent {
DBC.PRE.assertNotEmpty("poolName must be set!", poolName); DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
ExecutorService executor = this.executors.get(poolName); ExecutorService executor = this.executors.get(poolName);
if (executor == null) { if (executor == null) {
executor = Executors.newCachedThreadPool(new ThreadPoolFactory(poolName)); executor = Executors.newCachedThreadPool(new NamedThreadPoolFactory(poolName));
this.executors.put(poolName, executor); this.executors.put(poolName, executor);
} }
@ -117,7 +117,7 @@ public class StrolchAgent {
DBC.PRE.assertNotEmpty("poolName must be set!", poolName); DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
ExecutorService executor = this.executors.get(poolName); ExecutorService executor = this.executors.get(poolName);
if (executor == null) { if (executor == null) {
executor = Executors.newSingleThreadExecutor(new ThreadPoolFactory(poolName)); executor = Executors.newSingleThreadExecutor(new NamedThreadPoolFactory(poolName));
this.executors.put(poolName, executor); this.executors.put(poolName, executor);
} }
@ -137,7 +137,7 @@ public class StrolchAgent {
DBC.PRE.assertNotEmpty("poolName must be set!", poolName); DBC.PRE.assertNotEmpty("poolName must be set!", poolName);
ScheduledExecutorService executor = this.scheduledExecutors.get(poolName); ScheduledExecutorService executor = this.scheduledExecutors.get(poolName);
if (executor == null) { if (executor == null) {
executor = Executors.newScheduledThreadPool(4, new ThreadPoolFactory(poolName)); executor = Executors.newScheduledThreadPool(4, new NamedThreadPoolFactory(poolName));
this.scheduledExecutors.put(poolName, executor); this.scheduledExecutors.put(poolName, executor);
} }

View File

@ -1,19 +1,17 @@
package li.strolch.runtime; package li.strolch.utils;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Simple {@link ThreadFactory} with which the thread pool name can be defined * Simple {@link ThreadFactory} which allocates as a pool and has a name for each pool
*
* @see java.util.concurrent.Executors.DefaultThreadFactory
*/ */
public class ThreadPoolFactory implements ThreadFactory { public class NamedThreadPoolFactory implements ThreadFactory {
private final ThreadGroup group; private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1); private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String poolName; private final String poolName;
public ThreadPoolFactory(String poolName) { public NamedThreadPoolFactory(String poolName) {
SecurityManager s = System.getSecurityManager(); SecurityManager s = System.getSecurityManager();
this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); this.group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.poolName = poolName + "-"; this.poolName = poolName + "-";