Jef*_*erg 10 java multithreading threadpool
我正在使用ThreadPoolExecutorJava来管理大量正在运行的线程.我创建了自己的简单,ThreadFactory所以我可以给线程更好的名字.
问题是,首次创建线程池时,名称在线程中设置,并且不与线程池实际运行的任务相关联.我理解这一点......我的Runnables和Callables - 尽管它们有名字 - 实际上是从ThreadPoolExecutor运行线程中提取的一个层次.
StackOverflow上还有一些关于为ThreadPoolExecutor线程池创建名称的问题.(请参阅如何为可调用线程命名?以及如何在Java中命名线程池的线程.)
我想知道的是:有没有人有一个很好的解决方案来保持线程池线程的名称与它实际运行的Runnable同步?
即如果我打电话Thread.getCurrentThread().getName(),我想它不会重返顶级线程池的名称,而是可赎回/名称Runnable的该线程当前正在运行.
由于这主要是为了调试和日志记录的目的,我试图避免一个解决方案,涉及我将新代码放入可能提交给ThreadPoolExecutor的每个Runnable中 - 我只是将一些代码放入ThreadFactory或包装ThreadPoolExecutor本身,以便在一个地方完成更改.如果不存在这样的解决方案,我可能不会打扰,因为它不是关键任务.
开始编辑为了澄清,我知道我可以把一个Thread.currentThread().setName( "my runnable name" );作为每个Runnable的run方法的第一行,但我试图避免这样做.我在这里是一个完美主义者,我意识到这一点,所以如果人们想对这个问题发表评论并告诉我,我不会被冒犯.结束编辑
我想,我的另一个问题是人们是否认为做这样的事情是个坏主意.我是否应该像这样更新线程池名称?
谢谢你的任何建议!
Joh*_*int 15
创建一个覆盖beforeExecute方法的ThreadPoolExecutor.
private final ThreadPoolExecutor executor = new ThreadPoolExecutor (new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()){
protected void beforeExecute(Thread t, Runnable r) {
t.setName(deriveRunnableName(r));
}
protected void afterExecute(Runnable r, Throwable t) {
Thread.currentThread().setName("");
}
protected <V> RunnableFuture<V> newTaskFor(final Runnable runnable, V v) {
return new FutureTask<V>(runnable, v) {
public String toString() {
return runnable.toString();
}
};
};
}
Run Code Online (Sandbox Code Playgroud)
derveRunnableName()也许不确定究竟toString()会怎么样?
编辑:Thread.currentThread()实际上是在beforeExecute中设置的线程,它调用afterExecute.您可以引用Thread.currentThread(),然后在afterExecute中设置名称.这在javadocs中有说明
/**
* Method invoked upon completion of execution of the given Runnable.
* This method is invoked by the thread that executed the task. If
* non-null, the Throwable is the uncaught <tt>RuntimeException</tt>
* or <tt>Error</tt> that caused execution to terminate abruptly.
*
* <p><b>Note:</b> When actions are enclosed in tasks (such as
* {@link FutureTask}) either explicitly or via methods such as
* <tt>submit</tt>, these task objects catch and maintain
* computational exceptions, and so they do not cause abrupt
* termination, and the internal exceptions are <em>not</em>
* passed to this method.
*
* <p>This implementation does nothing, but may be customized in
* subclasses. Note: To properly nest multiple overridings, subclasses
* should generally invoke <tt>super.afterExecute</tt> at the
* beginning of this method.
*
* @param r the runnable that has completed.
* @param t the exception that caused termination, or null if
* execution completed normally.
*/
protected void afterExecute(Runnable r, Throwable t) { }
Run Code Online (Sandbox Code Playgroud)
编辑 TPE会将Runnable包装在FutureTask中,因此要支持toString您可以覆盖的方法newTaskFor并创建自己的包装FutureTask.