Tukar nama benang
Untuk memudahkan pembacaan log dan lambakan benang, nama benang boleh disesuaikan. Ini boleh dicapai dengan menggunakan ThreadFactory apabila mencipta ExecutorService. Terdapat banyak pelaksanaan antara muka ThreadFactory dalam perpustakaan utiliti popular:
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);
Walaupun ForkJoinPool tidak menggunakan antara muka ThreadFactory, ia juga menyokong penamaan semula benang:
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);
Buang benang dengan Banding lalai dengan penamaan:
"pool-1-thread-3" #14 prio=5 os_prio=31 tid=0x00007fc06b19f000 nid=0x5703 runnable [0x0000700001ff9000] java.lang.Thread.State: RUNNABLE at 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: RUNNABLE at 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: RUNNABLE at com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute(TaskHandler.java:16) ...
Bandingkan dengan penamaan tersuai:
"task-handler-thread-1" #14 prio=5 os_prio=31 tid=0x00007fb49c9df000 nid=0x5703 runnable [0x000070000334a000] java.lang.Thread.State: RUNNABLE at 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: RUNNABLE at 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: RUNNABLE at com.github.sorokinigor.article.tipsaboutconcurrency.setthreadsname.TaskHandler.compute(TaskHandler.java:16) ...
Bayangkan mungkin terdapat lebih daripada 3 utas.
Atas ialah kandungan terperinci Bagaimana untuk menukar nama benang dalam java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!