1、使用Executors工具类下的线程池方式创建的不足之处是使用了无界队列,如果任务执行时间过长,且任务数多的情况,可能会造成OOM
2、自定义线程池,示例如下:
static int thread_count = Runtime.getRuntime().availableProcessors(); static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("name-pool-%d").build(); static ExecutorService pool = new ThreadPoolExecutor(thread_count,thread_count * 2 + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(2048), threadFactory); pool.execute(new SubThread(contract)); pool.shutdown(); pool.awaitTermination(30, TimeUnit.SECONDS);