package com.ray.test;
import com.alibaba.fastjson.TypeReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest {
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("主线程开始:"+Thread.currentThread().getName());
ThreadTest test = new ThreadTest();
test.fun4();
System.out.println("主线程结束:"+Thread.currentThread().getName());
}
public void fun1() throws ExecutionException, InterruptedException {
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
System.out.println("当前线程:"+Thread.currentThread().getName());
int i = 10 / 2;
System.out.println("结果运行:" + i);
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getName());
int i = 10 / 2;
System.out.println("结果运行:" + i);
return i;
}, executor).exceptionally(throwable -> {
return 10;
});
System.out.println(future2.get());
CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getName());
int i = 10 / 2;
System.out.println("结果运行:" + i);
return i;
}, executor).handle((result, throwable) -> {
if(result != null){
return result * 2;
}
if(throwable != null){
return 20;
}
return result;
});
System.out.println(future3.get()+"--future3");
}
public void fun2() throws ExecutionException, InterruptedException {
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getName());
int i = 10 / 2;
return i;
}, executor).thenAccept(result -> {
System.out.println("--当前线程:" + Thread.currentThread().getName());
System.out.println("thenAcceptAsync 获取上一步结果,无返回值,result:" + result);
}).thenRunAsync(() -> {
System.out.println("--当前线程:" + Thread.currentThread().getName());
System.out.println("thenRunAsync 无结果,无返回值");
}, executor).thenApplyAsync((result) -> {
return result + "haha:thenApplyAsync获取返回值并处理返回";
}, executor);
System.out.println(future2.get());
}
public void fun4() throws ExecutionException, InterruptedException {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息……");
return "hello.jpg";
}, executor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性……");
return "黑色 2+64G";
}, executor);
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("查询商品的介绍……");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "apple";
}, executor);
CompletableFuture<Void> future4 = CompletableFuture.allOf(future1, future2, future3);
future4.get();
System.out.println(future1.get() + future2.get() + future3.get());
}
public void item(Long skuId) throws ExecutionException, InterruptedException {
SkuItemVo skuItemVo = new SkuItemVo();
CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
SkuInfoEntity info = getById(skuId);
skuItemVo.setInfo(info);
return info;
}, executor);
CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((result) -> {
Long spuId = result.getSpuId();
List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(spuId);
skuItemVo.setSaleAttr(saleAttrVos);
}, executor);
CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((result) -> {
Long spuId = result.getSpuId();
SpuInfoDescEntity desc = spuInfoDescService.getById(spuId);
skuItemVo.setDesc(desc);
}, executor);
CompletableFuture<Void> baseFuture = infoFuture.thenAcceptAsync((result) -> {
Long spuId = result.getSpuId();
Long catalogId = result.getCatalogId();
List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
skuItemVo.setGroupAttrs(attrGroupVos);
}, executor);
CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
skuItemVo.setImages(images);
}, executor);
CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
R r = seckillFeignService.getSkuSeckillInfo(skuId);
if (r.getCode() == 0) {
SeckillSkuInfoVo seckillSkuRedisVo = r.getData(new TypeReference<SeckillSkuInfoVo>() {
});
skuItemVo.setSeckillSkuInfoVo(seckillSkuRedisVo);
}
}, executor);
CompletableFuture.allOf(saleAttrFuture, descFuture, baseFuture, imagesFuture, seckillFuture).get();
return skuItemVo;
}
public void fun3() throws ExecutionException, InterruptedException {
CompletableFuture<Object> future3_1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1开始。当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("任务1结束。结果运行:" + i);
return i;
}, executor);
CompletableFuture<Object> future3_2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2开始。当前线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务2结束...");
return "hello";
}, executor);
future3_1.runAfterBothAsync(future3_2, ()->{
System.out.println("任务3开始。当前线程:" + Thread.currentThread().getId());
}, executor);
future3_1.thenAcceptBothAsync(future3_2, (f1, f2)->{
System.out.println("任务3开始。之前的结果:f1=" + f1 + ";f2=" + f2);
}, executor);
CompletableFuture<String> future3_3 = future3_1.thenCombineAsync(future3_2, (f1, f2) -> {
return f1 + ": " + f2 + "->ww";
}, executor);
System.out.println(future3_3.get());
future3_1.runAfterEitherAsync(future3_2, () -> {
System.out.println("任务3开始执行。");
}, executor);
future3_1.acceptEitherAsync(future3_2, (result) -> {
System.out.println("任务3开始执行。" + result);
}, executor);
CompletableFuture<String> future3_4 = future3_1.applyToEitherAsync(future3_2, (result) -> {
System.out.println("任务3开始执行。" + result);
return result.toString() + "哈哈";
}, executor);
System.out.println(future3_4.get());
}
}