Java 如何在ExecutorService生成的线程名称中添加前缀
我有JDK1.7的java代码,如下所示,它是并行线程基础实现 ExecutorService ExecutorService=Executors.newFixedThreadPool(currentRecordSize); executorService.execute((Runnable)someobject) 在日志中,我得到的线程名称如下 池2线程1 池-2-线程-2 池-1-线程-1 池-1-线程-2Java 如何在ExecutorService生成的线程名称中添加前缀,java,multithreading,executorservice,Java,Multithreading,Executorservice,我有JDK1.7的java代码,如下所示,它是并行线程基础实现 ExecutorService ExecutorService=Executors.newFixedThreadPool(currentRecordSize); executorService.execute((Runnable)someobject) 在日志中,我得到的线程名称如下 池2线程1 池-2-线程-2 池-1-线程-1 池-1-线程-2 我想用一些字符串作为它们的后缀,您可以使用自定义线程工厂,例如在Ehcache中,有
我想用一些字符串作为它们的后缀,您可以使用自定义线程工厂,例如在Ehcache中,有一个是这样实现的:
public class NamedThreadFactory implements ThreadFactory {
private static AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
/**
* Constructor accepting the prefix of the threads that will be created by this {@link ThreadFactory}
*
* @param namePrefix
* Prefix for names of threads
*/
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
/**
* Returns a new thread using a name as specified by this factory {@inheritDoc}
*/
public Thread newThread(Runnable runnable) {
return new Thread(runnable, namePrefix + " thread-" + threadNumber.getAndIncrement());
}
}
ExecutorService executor = Executors.newFixedThreadPool(currentRecordSize, new NamedThreadFactory("Your prefix here"));
然后,您可以通过以下方式创建遗嘱执行人:
public class NamedThreadFactory implements ThreadFactory {
private static AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
/**
* Constructor accepting the prefix of the threads that will be created by this {@link ThreadFactory}
*
* @param namePrefix
* Prefix for names of threads
*/
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
/**
* Returns a new thread using a name as specified by this factory {@inheritDoc}
*/
public Thread newThread(Runnable runnable) {
return new Thread(runnable, namePrefix + " thread-" + threadNumber.getAndIncrement());
}
}
ExecutorService executor = Executors.newFixedThreadPool(currentRecordSize, new NamedThreadFactory("Your prefix here"));
我的代码带有@guilloum逻辑。我唯一想做的是AtomicInteger字段应该是类级别的,而不是静态的,因为在每个循环中,新池都是按照我的逻辑创建的
public void run() {
log.info(name + " Started");
ExecutorService executorService = null;
while (true) {
try {
List<HashMap<String, String>> rows = QueryFromDB;
int currentRecordSize = rows.size();
if (currentRecordSize > 0) {
NamedThreadFactory threadFactory = new NamedThreadFactory(name);
log.info(" *** " + name + " Initializing Executor . Received " + rows.size() + " txns.");
if (currentRecordSize < threadPoolSize) {
//executorService = Executors.newFixedThreadPool(currentRecordSize);
executorService = Executors.newFixedThreadPool(currentRecordSize, threadFactory);
} else {
//executorService = Executors.newFixedThreadPool(threadPoolSize);
executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
}
for (HashMap<String, String> row : rows) {
MyClass obj = fromsomeclassmethod;
if (obj instanceof Runnable) {
executorService.execute((Runnable) obj);
} else {
obj.SomeMethod(..);
}
Thread.sleep(ThreadExecutorSleep);//some minor sleep like 10 miliseconds
}
if (!(executorService.isShutdown())) {
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
}
}
Thread.sleep(ADMSProcessor.fetchQueInterval);//1 second sleep
} catch (Exception ex) {
log.fatal("Exception in " + name + " Thread :" + ex);
}
}
}
public class NamedThreadFactory implements ThreadFactory {
//private static AtomicInteger threadNumber = new AtomicInteger(1);
private AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
/**
* Constructor accepting the prefix of the threads that will be created by this {@link ThreadFactory}
*
* @param namePrefix
* Prefix for names of threads
*/
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
/**
* Returns a new thread using a name as specified by this factory {@inheritDoc}
*/
public Thread newThread(Runnable runnable) {
return new Thread(runnable, namePrefix + " thread-" + threadNumber.getAndIncrement());
}
}
public void run(){
日志信息(名称+“已启动”);
ExecutorService ExecutorService=null;
while(true){
试一试{
列表行=QueryFromDB;
int currentRecordSize=rows.size();
如果(currentRecordSize>0){
NamedThreadFactory threadFactory=新的NamedThreadFactory(名称);
log.info(“***”+name+“初始化执行器.接收”+rows.size()+“txns”);
如果(currentRecordSize
也许这可以帮助您:只需创建一个ThreadFactory并将其传递给ExecuterService我可以设置私有静态AtomicInteger threadNumber=new AtomicInteger(1);作为实例方法,因为这是在某种后台工作中完成的,在这种工作中,这将根据基于Factory的计数进行设置。我在回答中给出了详细信息(仅供参考)。请建议是否有意义。我基本上是一个C活跃的开发人员,十年前做过java开发。所以需要位帮助是的,threadNumber可以是一个实例变量,而不是一个静态字段,这只是一个示例。