Reducing code size: A common thread factory implementation

Issue #13 closed
Markus KARG created an issue

Several parts of Babbler are using the same code again and again:

    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, "...thread name...");
            thread.setDaemon(true);
            return thread;
        }
    });

It makes sense to use a common implementation, like

public static final ThreadFactory createNamedThreadFactory(final String threadName) { return new ThreadFactory() { @Override public final Thread newThread(final Runnable r) { final Thread thread = new Thread(r, threadName); thread.setDaemon(true); return thread; } } }

Less code = less memory consumption, less load time, less gc length, less possible failure modes, higher execution speed :-)

Comments (4)

  1. Log in to comment