public class ThreadUtils {
private static final ExecutorService es = Executors.newFixedThreadPool(10);
public static void execute(Runnable runnable) {
es.submit(runnable);
}
}
ThreadUtils.execute(() -> dothing());
改进型:
public class ThreadUtils {
private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
private static ExecutorService pool = new ThreadPoolExecutor(5, 500,
10L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
//private static final ExecutorService es = Executors.newFixedThreadPool(10);
public static void execute(Runnable runnable) {
pool.submit(runnable);
}
}