NamedThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { final Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.getPriority() != priority) { t.setPriority(priority); } return t; } NamedThreadFactory(Class<?> clazz);  NamedThreadFactory(Class<?> clazz, int priority);  NamedThreadFactory(String name);  NamedThreadFactory(String name, int priority ); @Override Thread newThread(Runnable r);  }
@Test public void testNewThread() throws Exception { String threadName = "my-named-thread"; NamedThreadFactory namedThreadFactory = new NamedThreadFactory( threadName, Thread.MIN_PRIORITY ); ExecutorService executorService = Executors.newFixedThreadPool( 1, namedThreadFactory ); final CountDownLatch countDownLatch = new CountDownLatch( 1 ); final AtomicReference<String> actualThreadName = new AtomicReference<>( null ); final AtomicInteger actualThreadPriority = new AtomicInteger( Integer.MIN_VALUE ); executorService.submit( new Runnable() { @Override public void run() { actualThreadName.set( Thread.currentThread().getName() ); actualThreadPriority.set( Thread.currentThread().getPriority() ); countDownLatch.countDown(); } } ); countDownLatch.await();; assertEquals( Thread.MIN_PRIORITY, actualThreadPriority.get() ); assertEquals( "my-named-thread-thread-1", actualThreadName.get() ); }