CollectorThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable runnable) { StringBuilder nameSb = new StringBuilder(); nameSb.append(namePrefix); nameSb.append(threadNumber.getAndIncrement()); Thread t = new Thread(group, runnable, nameSb.toString(), 0); if (t.isDaemon()) { t.setDaemon(false); } if (t.getPriority() != Thread.NORM_PRIORITY) { t.setPriority(Thread.NORM_PRIORITY); } return t; } CollectorThreadFactory(); @Override Thread newThread(Runnable runnable); static final String COLLECTOR_THREAD_NAME_PREFIX; static final String COLLECTOR_THREAD_NAME_SEPARATOR; static final String COLLECTOR_THREAD_NAME_MIDDLE; }
@Test public void testNewThread01() throws Exception { Thread ownThread = new Thread(); ownThread.setDaemon(true); CollectorThreadFactory factory = new CollectorThreadFactory(); Thread target = factory.newThread(ownThread); assertFalse(target.isDaemon()); }
@Test public void testNewThread02() throws Exception { Thread ownThread = new Thread(); ownThread.setPriority(Thread.MAX_PRIORITY); CollectorThreadFactory factory = new CollectorThreadFactory(); Thread target = factory.newThread(ownThread); assertEquals(Thread.NORM_PRIORITY, target.getPriority()); }