springboot 多线程的使用( 二 )


  • 声明
private final static ArrayBlockingQueue<Runnable> WORK_QUEUE= new ArrayBlockingQueue<>(9);// handler – 由于达到线程边界和队列容量而阻塞执行时使用的处理程序private final static RejectedExecutionHandler HANDLER=new ThreadPoolExecutor.CallerRunsPolicy();private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(16, 16, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
  • 使用,通过 get() 方法获取他的返回值
//商品属性Callable<List> listCallable=()->goodsAttributeService.getGoodsAttributeList(litemallGoods.getGoodsSn());//商品规格Callable<Object> specificationCallable =()->specificationService.getGoodsSpecification(litemallGoods.getGoodsSn());FutureTask<List> goodsAttributeListTask = new FutureTask<>(listCallable);FutureTask<Object> objectCallableTask = new FutureTask<>(specificationCallable);executorService.submit(goodsAttributeListTask);executorService.submit(objectCallableTask);

经验总结扩展阅读