public static ThreadFactory createThreadFactory(final String pattern,
                                                    final boolean daemon) {
        return new ThreadFactory() {
            private final AtomicLong threadEpoch = new AtomicLong(0);

            @Override
            public Thread newThread(Runnable r) {
                String threadName;
                if (pattern.contains("%d")) {
                    threadName = String.format(pattern, threadEpoch.addAndGet(1));
                } else {
                    threadName = pattern;
                }
                Thread thread = new Thread(r, threadName);
                thread.setDaemon(daemon);
                return thread;
            }
        };
    }
@Test
    public void testThreadNameWithoutNumberDemon() {
        Thread daemonThread = ThreadUtils.createThreadFactory(THREAD_NAME, true).newThread(EMPTY_RUNNABLE);
        try {
            assertEquals(THREAD_NAME, daemonThread.getName());
            assertTrue(daemonThread.isDaemon());
        } finally {
            try {
                daemonThread.join();
            } catch (InterruptedException e) {
                // can be ignored
            }
        }
    }