static class SelfDefinedThreadFactory implements ThreadFactory { final AtomicInteger threadNumber = new AtomicInteger(1); final String namePrefix; SelfDefinedThreadFactory(String namePrefix) { this.namePrefix = namePrefix+"-"; } public Thread newThread(Runnable r) { Thread t = new Thread( r,namePrefix + threadNumber.getAndIncrement()); if (t.isDaemon()) t.setDaemon(true); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } }
默認的ThreadFactory是DefaultThreadFactory, 咱們能夠從新實現ThreadFactory接口,對Thread進行一些自定義的實現,好比線程的名稱或是線程的優先級。java