使用线程池可以提高系统的资源利用率、响应速度和并发控制,同时也简化了线程管理和资源管理的复杂性,是开发中常用的并发编程工具。
核心线程数(Core Pool Size):指定线程池中的核心线程数量,即线程池保持的最小线程数。即使线程处于空闲状态,核心线程也不会被回收。当有新的任务提交时,如果当前线程数小于核心线程数,将会创建新的线程来执行任务。
最大线程数(Maximum Pool Size):指定线程池中允许的最大线程数。当任务提交的数量超过核心线程数且工作队列已满时,线程池会创建新的线程来执行任务,直到达到最大线程数。超过最大线程数的任务将会被拒绝执行,可以根据具体需求选择合适的值。
空闲线程存活时间(Keep-Alive Time):当线程池中的线程数大于核心线程数,并且处于空闲状态时,空闲线程的存活时间。超过存活时间后,空闲线程将会被回收,以减少资源消耗。
阻塞队列(Blocking Queue):用于存储等待执行的任务的队列。当任务提交的数量超过核心线程数时,超过核心线程数的任务会被放入阻塞队列中等待执行。常用的阻塞队列有 ArrayBlockingQueue、LinkedBlockingQueue、SynchronousQueue 等,可以根据需求选择合适的队列类型和大小。
线程工厂(Thread Factory):用于创建线程的工厂类。线程池通过线程工厂来创建新的线程对象。可以自定义线程工厂来设置线程名称、优先级等属性。
拒绝策略(Rejected Execution Policy):当线程池无法接受新的任务时,即达到了最大线程数且阻塞队列已满时,定义了如何处理被拒绝的任务。常见的拒绝策略有抛出异常、丢弃任务、丢弃最旧的任务、将任务回退给调用者等。
核心线程处理:如果线程池中的线程数小于核心线程数,线程池会创建新的线程来执行任务。
阻塞队列存储:如果线程池中的线程数已经达到核心线程数,并且有新的任务提交,但是阻塞队列未满,线程池会将任务放入阻塞队列中等待执行。
创建非核心线程:如果线程池中的线程数已经达到核心线程数,并且阻塞队列已满,但是线程池中的线程数还没有达到最大线程数,线程池会创建新的非核心线程来执行任务。
执行拒绝策略:如果线程池中的线程数已经达到最大线程数,并且阻塞队列已满,此时线程池无法接受新的任务,会根据配置的拒绝策略来处理被拒绝的任务。常见的拒绝策略有抛出异常、丢弃任务、丢弃最旧的任务、将任务回退给调用者等。
如果线程池无法处理提交的任务,一般会采取以下几种处理方式:
抛出异常:使用默认的拒绝策略,将无法处理的任务抛出一个 RejectedExecutionException 异常。
丢弃任务:使用 ThreadPoolExecutor.DiscardPolicy 拒绝策略,直接丢弃无法处理的任务,不做任何处理。
丢弃最旧的任务:使用 ThreadPoolExecutor.DiscardOldestPolicy 拒绝策略,丢弃阻塞队列中最旧的任务,然后尝试再次提交当前任务。
调用者运行:使用 ThreadPoolExecutor.CallerRunsPolicy 拒绝策略,将任务退回给提交任务的线程来执行,由提交任务的线程直接执行该任务。
自定义拒绝策略:根据具体需求,可以自定义拒绝策略来处理无法处理的任务,例如将任务记录到日志中、进行异步处理等。
我们异步执行一个任务时,一般是用线程池Executor去创建。如果不需要有返回值,任务实现Runnable接口;如果需要有返回值,任务实现Callable接口,调用Executor的submit方法,再使用Future获取即可。如果多个线程存在依赖组合的话,则可以使用CompeletableFuture。
因为CompletableFuture实现了Future接口,我们先从Future接口说起
Future是Java5新加的一个接口定义了操作异步任务执行一些方法,如获取异步任务的执行结果、取消任务的执行、判断任务是否被取消、判断任务执行是否完毕等。,它提供了一种异步并行计算的功能。如果主线程需要执行一个很耗时的计算任务,我们就可以通过future把这个任务放到异步线程中执行。主线程继续处理其他任务,处理完成后,再通过Future获取计算结果。
如下例,假设我们有两个任务服务,一个是sku基本信息获取,一个是获取spu的销售属性组合。
public class GoodsSkuService {
public GoodsSkuInfo getGoodsSkuInfo(Long userId) throws InterruptedException {
Thread.sleep(300);//模拟调用耗时 一般是查数据库,或者远程调用返回的结果
}
}
public class GoodsSpuService {
public GoodsSpuInfo getGoodsSpu(long userId) throws InterruptedException {
Thread.sleep(500); //模拟调用耗时
}
}
接下来,我们来演示下,在主线程中是如何使用Future来进行异步调用的。
public class FutureTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//调用用户服务sku基本信息获取
FutureTask<GoodsSkuInfo> goodsSkuInfo= new FutureTask<>(new Callable<GoodsSkuInfo>() {
@Override
public GoodsSkuInfocall() throws Exception {
return goodsSkuInfoService.getGoodsSkuInfo(goodsId);
}
});
executorService.submit(userInfoFutureTask);
Thread.sleep(300); //模拟主线程其它操作耗时
FutureTask<GoodsSpuInfo> goodsSpuInfo= new FutureTask<>(new Callable<GoodsSpuInfo>() {
@Override
public GoodsSkuInfocall() throws Exception {
return goodsSpuInfoService.getGoodsSpuInfo(goodsId);
}
});
executorService.submit(medalInfoFutureTask);
GoodsSkuInfo goodsSkuInfo= userInfoFutureTask.get();//sku基本信息获取
GoodsSpuInfo goodsSpuInfo= medalInfoFutureTask.get();//获取spu的销售属性组合。
}
}
future+线程池异步配合,可以提高程序的执行效率,但是Future对于结果的获取,不是很友好,只能通过阻塞或者轮询的方式得到任务的结果。
阻塞的方式和异步编程的设计理念相违背,而轮询的方式会耗费无谓的CPU资源。因此,JDK8设计出CompletableFuture。CompletableFuture提供了一种观察者模式类似的机制,可以让任务执行完成后通知监听的一方。
CompletableFuture
上述例子可以改写成:
ublic class FutureTest {
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
//调用用户服务sku基本信息获取
FutureTask<GoodsSkuInfo> completablegoodsSkuInfo = CompletableFuture.supplyAsync(() -> goodsSkuInfoService.getGoodsSkuInfo(goodsId));
Thread.sleep(300); //模拟主线程其它操作耗时
FutureTask<GoodsSpuInfo> completablegoodsSpuInfo = CompletableFuture.supplyAsync(() -> goodsSpuInfoService.getGoodsSpuInfo(goodsId));
GoodsSkuInfo goodsSkuInfo= completablegoodsSkuInfo Future.get(2,TimeUnit.SECONDS);;//sku基本信息获取
GoodsSpuInfo goodsSpuInfo= completablegoodsSpuInfo Future.get();//获取spu的销售属性组合。
}
}
可以发现,使用CompletableFuture,代码简洁了很多。CompletableFuture的supplyAsync方法,提供了异步执行的功能,线程池也不用单独创建了。实际上,它CompletableFuture使用了默认线程池是ForkJoinPool.commonPool。
CompletionStage(不做详细叙述)
异步任务结束时,会自动回调某个对象的方法;
异步任务出错时,会自动回调某个对象的方法;
主线程设置好回调后,不再关心异步任务的执行,异步任务之间可以顺序执行。
CompletableFuture提供了几十种方法,辅助我们的异步任务场景。这些方法包括创建异步任务、任务异步回调、多个任务组合处理等方面。再次仅仅比较重要的四个核心的静态方法。
1、runAsync 无 返回值
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor)
代码示例
public class CompletableFutureDemo3{
public static void main(String[] args) throws ExecutionException, InterruptedException{
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName()+"\t"+"-----come in");
//暂停几秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("-----task is over");
});
System.out.println(future.get());
}
}
2、supplyAsync 有 返回值
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor)
代码示例
public class CompletableFutureDemo3{
public static void main(String[] args) throws ExecutionException, InterruptedException{
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "\t" + "-----come in");
//暂停几秒钟线程
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ThreadLocalRandom.current().nextInt(100);
});
System.out.println(completableFuture.get());
}
}
上述Executor executor参数说明
没有指定Executor的方法,直接使用默认的ForkJoinPool.commonPool() 作为它的线程池执行异步代码。如果指定线程池,则使用我们自定义的或者特别指定的线程池执行异步代码
public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action);
public CompletableFuture<T> whencompleteAsync(BiConsumer<? super T,? super Throwable> action);
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,super Throwable> action,Executor executor);
public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn);
whenComplete可以处理正常和异常的计算结果,exceptionally处理异常情况。
whenComplete和whenCompleteAsync的区别:
方法不以Async结尾,意味着Action使用相同的线程执行,而Async可能会使用其他线程执行(如果是使用相同的线程池,也可能会被同一个线程选中执行)
从Java8开始引入了CompletableFuture,它是Future的功能增强版,可以传入回调对象,当异步任务完成或者发生异常时,自动调用回调对象的回调方法
public class CompletableFutureDemo3{
public static void main(String[] args) throws Exception{
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "\t" + "-----come in");
int result = ThreadLocalRandom.current().nextInt(10);
//暂停几秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("-----计算结束耗时1秒钟,result: "+result);
if(result > 6){
int age = 10/0;
}
return result;
}).whenComplete((v,e) ->{
if(e == null){
System.out.println("-----result: "+v);
}
}).exceptionally(e -> {
System.out.println("-----exception: "+e.getCause()+"\t"+e.getMessage());
return -44;
});
//主线程不要立刻结束,否则CompletableFuture默认使用的线程池会立刻关闭:暂停3秒钟线程
try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
}
}
public <U> CompletableFuture<U> thenApply(Function<? super T,extends U> fn)
thenApply 方法:当一个线程依赖另一个线程时,获取上一个任务返回的结果,并返回当前
任务的返回值。
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn,Executor executor)
public Completionstage<Void> thenAccept(Consumer<? super T> action);
thenAccept 方法:消费处理结果。接收任务的处理结果,并消费处理,无返回结果。
public Completionstage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
public CompletionStage<Void> thenRun(Runnable action);
thenRun 方法:只要上面的任务执行完成,就开始执行 thenRun,只是处理完任务后,执行
thenRun 的后续操
public Completionstage<Void> thenRunAsync(Runnable action);
public Completionstage<Void> thenRunAsync(Runnable action,Executor executor )
带有 Async 默认是异步执行的。
public static CompletableFuture<Void>allof(CompletableFuture<?>... cfs);
public static CompletableFuture<Object>anyof(CompletableFuture<?>... cfs);
allOf:等待所有任务完成
anyOf:只要有一个任务完成
业务场景:
查询商品详情页的逻辑比较复杂,有些数据还需要远程调用,必然需要花费更多的时间。
//1,获取sku的基本信息 0.5s
//2.获取sku的图片信息 0.5s
//3.获取sku的促销信息 1s
//4.获取spu的所有销售属性 1s
//5.获取规格参数组及组下的规格参数 1.5s
//6.spu详情 1s
假如商品详情页的每个查询,需要如下标注的时间才能完成那么,用户需要 5.5s 后才能看到商品详情页的内容。很显然是不能接受的。如果有多个线程同时完成这 6 步操作,也许只需要 1.5s。
以下是实战项目中利用线程池加CompletableFuture 异步编排来优化代码一定程度上增大了接口的QPS。
@Override
public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
SkuItemVo skuItemVo = new SkuItemVo();
CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
//1、sku基本信息获取 pms_sku_info
SkuInfoEntity info = getById(skuId);
skuItemVo.setInfo(info);
return info;
}, executor);
CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(res -> {
//3、获取spu的销售属性组合
List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySkuId(res.getSkuId());
skuItemVo.setSaleAttr(saleAttrVos);
}, executor);
CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
//4、获取spu的介绍 pms_sku_info_desc
SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
skuItemVo.setDesc(spuInfoDescEntity);
}, executor);
CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(res -> {
//5、获取spu的规格参数信息
List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
skuItemVo.setGroupAttrs(attrGroupVos);
}, executor);
//2、sku的图片信息 pms_sku_images
CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
List<SkuImagesEntity> images = imagesService.getIamImagesBySkuId(skuId);
skuItemVo.setImages(images);
}, executor);
CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
//3、远程调用查询当前sku是否参与秒杀优惠活动
R skuSeckilInfo = seckillFeignService.getSkuSeckilInfo(skuId);
if (skuSeckilInfo.getCode() == 0) {
//查询成功
SeckillSkuVo seckilInfoData = skuSeckilInfo.getData("data", new TypeReference<SeckillSkuVo>() {
});
skuItemVo.setSeckillSkuVo(seckilInfoData);
if (seckilInfoData != null) {
long currentTime = System.currentTimeMillis();
if (currentTime > seckilInfoData.getEndTime()) {
skuItemVo.setSeckillSkuVo(null);
}
}
}
}, executor);
//等到所有任务都完成
CompletableFuture.allOf(saleAttrFuture,descFuture,baseAttrFuture,imageFuture,seckillFuture).get();
return skuItemVo;
}
上述代码是本此业务的核心代码,涉及微服务的远程调用等与CompletableFuture 无关的代码就给予忽视了