In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces java how to change the thread name, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Change thread name
To simplify log reading and thread dumps, you can customize the name of the thread. This can be done by using a ThreadFactory when creating an ExecutorService. There are many implementations of the ThreadFactory interface in popular utility libraries:
Com.google.common.util.concurrent.ThreadFactoryBuilde+r in Guava. Org.springframework.scheduling.concurrent.CustomizableThreadFactory in Spring. Org.apache.commons.lang3.concurrent.BasicThreadFactory in Apache Commons Lang 3.ThreadFactory threadFactory = new BasicThreadFactory.Builder () .namingPattern ("computation-thread-%d") .build (); ExecutorService executorService = Executors.newFixedThreadPool (numberOfThreads, threadFactory)
Although ForkJoinPool does not use the ThreadFactory interface, it also supports renaming of threads:
ForkJoinPool.ForkJoinWorkerThreadFactory forkJoinThreadFactory = pool-> {ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread (pool); thread.setName ("computation-thread-" + thread.getPoolIndex ()); return thread;}; ForkJoinPool forkJoinPool = new ForkJoinPool (numberOfThreads, forkJoinThreadFactory, null, false)
Compare the thread dump to the default naming:
"pool-1-thread-3" # 14 prio=5 os_prio=31 tid=0x00007fc06b19f000 nid=0x5703 runnable [0x0000700001ff9000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute (TaskHandler.java:16)... "pool-2-thread-3" # 15 prio=5 os_prio=31 tid=0x00007fc06aa10800 nid=0x5903 runnable [0x00007000020fc000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.HealthCheckCallback.recordFailure (HealthChecker.java:21) at com.github.sorokinigor.article.tipsaboutconcurrency. Setthreadsname.HealthChecker.check (HealthChecker.java:9)... "pool-1-thread-2" # 12 prio=5 os_prio=31 tid=0x00007fc06aa10000 nid=0x5303 runnable [0x0000700001df3000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute (TaskHandler.java:16).
Compare with custom naming:
"task-handler-thread-1" # 14 prio=5 os_prio=31 tid=0x00007fb49c9df000 nid=0x5703 runnable [0x000070000334a000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute (TaskHandler.java:16)... "authentication-service-ping-thread-0" # 15 prio=5 os_prio=31 tid=0x00007fb49c9de000 nid=0x5903 runnable [0x0000700003247000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.HealthCheckCallback.recordFailure (HealthChecker.java:21) at com.github.sorokinigor.article. Tipsaboutconcurrency.setthreadsname.HealthChecker.check (HealthChecker.java:9)... "task-handler-thread-0" # 12 prio=5 os_prio=31 tid=0x00007fb49b9b5000 nid=0x5303 runnable [0x0000700003144000] java.lang.Thread.State: RUNNABLEat com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute (TaskHandler.java:16).
Imagine that there might be more than three threads.
Thank you for reading this article carefully. I hope the article "how to change the thread name of java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.