1.集成Thread
2.实现Rubbable接口
3.实现Callable接口 配合FuturTask
class myThread implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("come in");
return 1024;
}
}
public class CallableDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask = new FutureTask(new myThread());
new Thread(futureTask, "AA").start();
Integer o = futureTask.get();
System.out.println(o);
}
}
4.线程池
- 线程池三个常用的方式 (JDK自带的 实际开发我们不用) 因为不能控制资源假设高并发同时创建了很多的线程 服务器直接瘫痪…
//创建一个固定线程的线程池
ExecutorService executorService = Executors.newFixedThreadPool(3);
//创建一个单个线程的线程池
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
//创建一个会自动扩容的线程池
ExecutorService executorService3 = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 10; i++) {
executorService3.execute(() -> {
System.out.println(Thread.currentThread().getName() + "*****");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
executorService3.shutdown();
}
- 线程池的七个重要参数
这里注意: 我们看源码的时候会发现只有五个参数 这里我为什么会说是七个参数呢
点进去看 调用this传了七个参数
参数2 最大值为 Integer.MAX_VALUE 2147483647
解释参数3 4。 比如说这个方法,当大量请求进来了他就会创建好多个线程,而后面一定时间单位没有那么多请求了,他就会关闭达到一定时间没有用过的线程(比如太忙被叫过来加班,后来不忙了提前下班了。)。//创建一个会自动扩容的线程池 ExecutorService executorService3 = Executors.newCachedThreadPool();
参数7. 就是我们最大线程数和我们阻塞队列全部满了,将会启动拒绝策略。
四种拒绝策略
手写改造线程池
创建一个核心线程数为2 最大为5 阻塞队列为3 超时1s销毁新增线程的线程池 阻塞策略抛异常
ExecutorService pool = new ThreadPoolExecutor(
2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
怎么合理配置线程池
首先,查看自己电脑的核数System.out.println(Runtime.getRuntime().availableProcessors());
核数+1 =最大线程数
completableFuture 异步编排
这6步骤的执行有依赖关系 我们后边几步需要前面的执行结果 可以用异步编排实现
CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程" + Thread.currentThread().getName());
int i = 10 / 4;
System.out.println("运行结果" + i);
return i;
}, executorService);
CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程" + Thread.currentThread().getName());
int i = 10 / 4;
System.out.println("运行结果" + i);
return i;
}, executorService).thenRunAsync(()->{
System.out.println("任务2启动了 ... 结束");
},executorService);
任务1和任务2都执行完了 再执行任务3
future01.thenAcceptBothAsync() 这个可以接收前面的结果
future01.thenCombineAsync() 这个可以定义返回值
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程启动了..." + Thread.currentThread().getName());
int i = 10 / 4;
System.out.println("运行结果" + i);
System.out.println("任务1线程结束了..." + Thread.currentThread().getName());
return i;
}, executorService);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程启动了..." + Thread.currentThread().getName());
System.out.println("任务2线程结束了..." + Thread.currentThread().getName());
return "hello";
}, executorService);
future01.thenAcceptBothAsync(future02,(f1,f2)->{
System.out.println("任务3开始...");
System.out.println("之前的结果" + f1 + f2);
},executorService);
A和B只要有一个执行完成就执行C
多任务组合
配置并使用线程池
package com.gulimall.product.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class MyThreadPoolConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties) {
//配置类代码
return new ThreadPoolExecutor(
threadPoolConfigProperties.getCoreSize(),
threadPoolConfigProperties.getMaxSize(),
threadPoolConfigProperties.getKeepActiveTime(),
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(100000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
}
}
为了方便配置 我们可以自定义配置文件 并且添加下面依赖在配置文件里会有提示
<!--属性的提示工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
package com.gulimall.product.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
private Integer coreSize;
private Integer maxSize;
private Integer keepActiveTime;
}
@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> imageFuture = infoFuture.thenAcceptAsync((res) -> {
//2 sku图片信息
List<SkuImagesEntity> skuImagesEntities = skuImagesService.getImagesBySkuId(skuId);
skuItemVo.setImages(skuImagesEntities);
}, executor);
CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res) -> {
//3 获取spu销售属性组合
List<ItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
skuItemVo.setSaleAttr(saleAttrVos);
}, executor);
CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
//4 获取spu介绍 pms_spu_info_desc
SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
skuItemVo.setDesc(spuInfoDescEntity);
}, executor);
CompletableFuture<Void> groupAttrFuture = infoFuture.thenAcceptAsync((res) -> {
//5 获取spu规格参数信息
List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
skuItemVo.setGroupAttrs(attrGroupVos);
}, executor);
//等待所有的异步任务都完成才返回我们的结果
CompletableFuture.allOf(imageFuture,attrFuture,descFuture,groupAttrFuture).get();
// 6.查询当前sku是否参与秒杀优惠
return skuItemVo;
}