How to set thread pool name in Java
This article introduces how to set the thread pool name in Java, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
The first CustomizableThreadFactory
CustomizableThreadFactory provided by the Spring framework.
ThreadFactory springThreadFactory = new CustomizableThreadFactory ("springThread-pool-")
ExecutorService exec = new ThreadPoolExecutor (1,1)
0L, TimeUnit.MILLISECONDS
New LinkedBlockingQueue (10), springThreadFactory)
Exec.submit (()-> {
Logger.info ("- what is the color in memory--")
});
The second kind of ThreadFactoryBuilder
The ThreadFactoryBuilder provided by the Google guava utility class, created using a chained method.
ThreadFactory guavaThreadFactory = new ThreadFactoryBuilder () .setNameFormat ("retryClient-pool-") .build ()
ExecutorService exec = new ThreadPoolExecutor (1,1)
0L, TimeUnit.MILLISECONDS
New LinkedBlockingQueue (10), springThreadFactory)
Exec.submit (()-> {
Logger.info ("- what is the color in memory--")
});
The third kind of BasicThreadFactory
BasicThreadFactory provided by Apache commons-lang3.
ThreadFactory basicThreadFactory = new BasicThreadFactory.Builder ()
.namingPattern ("basicThreadFactory-") .build ()
ExecutorService exec = new ThreadPoolExecutor (1,1)
0L, TimeUnit.MILLISECONDS
New LinkedBlockingQueue (10), springThreadFactory)
Exec.submit (()-> {
Logger.info ("- what is the color in memory--")
});
Summary
The ultimate essence is to set the name of the java.lang.Thread#name, the details of the source code interested can check.
Final Thread thread = new Thread ()
Thread.setName (name)
On how to set the thread pool name in Java to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.