Skip to content

Confgurable executor service for IRP queue #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
new thread factory for named daemon threads
  • Loading branch information
colinrgodsey committed Apr 27, 2014
commit 401f007658b0fb65142adaef90975573c13c04e2
34 changes: 32 additions & 2 deletions src/main/java/org/usb4java/javax/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Properties;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Configuration.
Expand Down Expand Up @@ -41,11 +42,40 @@ final class Config

/** The executor service factory. */
private ExecutorServiceProvider executorService = new ExecutorServiceProvider() {
private final AtomicInteger poolNumber = new AtomicInteger(1);

class LocalThreadFactory extends Object implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;

LocalThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "usb4java-irp-" +
poolNumber.getAndIncrement() +
"-thread-";
}

public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
t.setDaemon(true);
if (t.getPriority() != Thread.MAX_PRIORITY)
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
}

public ExecutorService newExecutorService() {
/* The default executor is a pool of max 1 thread, with 3s timeout. */
return (new ThreadPoolExecutor(0, 1,
ThreadPoolExecutor es = new ThreadPoolExecutor(0, 1,
3L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()));
new LinkedBlockingQueue<Runnable>());
es.setThreadFactory(new LocalThreadFactory());
return es;
}
};

Expand Down