线程池配置核心业务线程池和非核心业务线程池 核心业务的线程不够用 可以停掉非核心业务占用的线程
application.properties
#线程池配置
gmall.pool.coreSize=8
gmall.pool.maximumPoolSize=100
gmall.pool.queueSize=1000000
PoolProperties :读取配置文件的值
package com.xiepanpan.gmall.portal.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author: xiepanpan
* @Date: 2020/2/27
* @Description: 线程池配置参数
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "gmall.pool")
public class PoolProperties {
private Integer coreSize;
private Integer maximumPoolSize;
private Integer queueSize;
}
ThreadPoolConfig :配置当前系统的线程池信息
package com.xiepanpan.gmall.portal.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author: xiepanpan
* @Date: 2020/2/27
* @Description: 配置当前系统的线程池信息
*/
@Configuration
public class ThreadPoolConfig {
/**
* 核心线程
* @param poolProperties
* @return
*/
@Bean("mainThreadPoolExecutor")
public ThreadPoolExecutor mainThreadPoolExecutor(PoolProperties poolProperties) {
LinkedBlockingDeque<Runnable> deque = new LinkedBlockingDeque<>(poolProperties.getQueueSize());
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolProperties.getCoreSize()
, poolProperties.getMaximumPoolSize(), 10,
TimeUnit.MINUTES, deque);
return threadPoolExecutor;
}
/**
* 非核心线程
* @param poolProperties
* @return
*/
@Bean("otherThreadPoolExecutor")
public ThreadPoolExecutor otherThreadPoolExecutor(PoolProperties poolProperties) {
LinkedBlockingDeque<Runnable> deque = new LinkedBlockingDeque<>(poolProperties.getQueueSize());
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolProperties.getCoreSize()
,poolProperties.getMaximumPoolSize(),10,TimeUnit.MINUTES,deque);
return threadPoolExecutor;
}
}
使用线程池
package com.xiepanpan.gmall.portal.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.xiepanpan.gmall.pms.service.ProductService;
import com.xiepanpan.gmall.to.CommonResult;
import com.xiepanpan.gmall.to.es.EsProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author: xiepanpan
* @Date: 2020/2/26
* @Description: 商品详情控制层
*/
@RestController
@Slf4j
public class ProductItemController {
@Reference
ProductService productService;
@Qualifier("mainThreadPoolExecutor")
@Autowired
ThreadPoolExecutor mainThreadPoolExecutor;
@Qualifier("otherThreadPoolExecutor")
@Autowired
ThreadPoolExecutor otherThreadPoolExecutor;
public EsProduct productInfo2(Long id){
CompletableFuture.supplyAsync(()->{
return "";
},mainThreadPoolExecutor).whenComplete((r,e)->{
log.info("处理结果"+r);
log.error("处理异常"+e);
});
//1、商品基本数据(名字介绍等) 100ms 异步
//2、商品的属性数据 300ms
new Thread(()->{
log.info("查属性信息");
}).start();
//3、商品的营销数据 SmsService 1s 500ms
new Thread(()->{
log.info("查营销信息");
}).start();
//4、商品的配送数据 WuliuService 2s 700ms
new Thread(()->{
log.info("查配送信息");
}).start();
//5、商品的增值服务数据 SaleService 1s 1s
new Thread(()->{
log.info("查增值信息");
}).start();
//otherThreadPoolExecutor.submit()
//8s 2.5s; 需要速度快。 开启异步化 最多1s,取决最长的服务调用。
//高并发系统的优化
//1、加缓存
//2、开异步
return null;
}
}
监控线程池:
package com.xiepanpan.gmall.portal.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author: xiepanpan
* @Date: 2020/2/28
* @Description: 监控线程池
*/
@RestController
public class ThreadPoolController {
@Qualifier("mainThreadPoolExecutor")
@Autowired
ThreadPoolExecutor threadPoolExecutor;
@GetMapping("/thread/status")
public Map threadPoolSize() {
Map<String,Object> map = new HashMap<>();
map.put("ActiveCount",threadPoolExecutor.getActiveCount());
map.put("CorePoolSize",threadPoolExecutor.getCorePoolSize());
return map;
}
}
来源:CSDN
作者:周杰伦本人
链接:https://blog.csdn.net/sdaujsj1/article/details/104547588