NamedThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable runnable) { String name = mPrefix + mThreadNum.getAndIncrement(); Thread ret = new Thread(mGroup, runnable, name, 0); ret.setDaemon(mDaemon); return ret; } NamedThreadFactory();  NamedThreadFactory(String prefix);  NamedThreadFactory(String prefix, boolean daemon); @Override Thread newThread(Runnable runnable); ThreadGroup getThreadGroup();  }
@Test public void testNewThread() throws Exception { NamedThreadFactory factory = new NamedThreadFactory(); Thread t = factory.newThread(Mockito.mock(Runnable.class)); assertThat(t.getName(), allOf(containsString("pool-"), containsString("-thread-"))); assertFalse(t.isDaemon()); assertSame(t.getThreadGroup(), Thread.currentThread().getThreadGroup()); }
@Test public void testPrefixAndDaemon() throws Exception { NamedThreadFactory factory = new NamedThreadFactory("prefix", true); Thread t = factory.newThread(Mockito.mock(Runnable.class)); assertThat(t.getName(), allOf(containsString("prefix-"), containsString("-thread-"))); assertTrue(t.isDaemon()); }