线程&异步

ExecutorService executorService = new ThreadPoolExecutor(10, 10,
60L, TimeUnit.SECONDS,
new ArrayBlockingQueue(10)); //推荐使用方法 手动创建线程,

ExecutorService executorService = Executors.newFixedThreadPool(50);   //    alibaba禁用的方法 可能会造成oom溢出


//通过下面的方式不仅可以避免OOM的问题,还可以自定义线程名称,更加方便的出错的时候溯源。

private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();

private static ExecutorService pool = new ThreadPoolExecutor(5, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

public static void main(String[] args) {

           for (int i = 0; i < Integer.MAX_VALUE; i++) {
                   pool.execute(new SubThread());

      }
}

 

posted @ 2021-12-21 09:42  悄悄地超越  阅读(19)  评论(0编辑  收藏  举报