1、为你的线程池设定名称,方便后续定位问题。
import com.google.common.util.concurrent.ThreadFactoryBuilder;
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat("Orders-%d")
.setDaemon(false)
.build();
final ExecutorService executorService =
Executors.newFixedThreadPool(10, threadFactory);
2、在你使用线程的时候可以临时指定线程名称,使用完之后可以改回原有线程的名称。方便定位问题。
private void process(String messageId) {
executorService.submit(() -> {
final Thread currentThread = Thread.currentThread();
final String oldName = currentThread.getName();
currentThread.setName("Processing-" + messageId);
try {
//real logic here...
} finally {
currentThread.setName(oldName);
}
});
}