线程池的监控

线程池配置核心业务线程池和非核心业务线程池 核心业务的线程不够用 可以停掉非核心业务占用的线程
在这里插入图片描述

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值