前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线程池的监控

线程池的监控

作者头像
周杰伦本人
发布2022-10-25 15:48:31
3150
发布2022-10-25 15:48:31
举报
文章被收录于专栏:同步文章同步文章

线程池配置核心业务线程池和非核心业务线程池 核心业务的线程不够用 可以停掉非核心业务占用的线程

在这里插入图片描述
在这里插入图片描述

application.properties

代码语言:javascript
复制
#线程池配置
gmall.pool.coreSize=8
gmall.pool.maximumPoolSize=100
gmall.pool.queueSize=1000000

PoolProperties :读取配置文件的值

代码语言:javascript
复制
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 :配置当前系统的线程池信息

代码语言:javascript
复制
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;
    }
}

使用线程池

代码语言:javascript
复制
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;
    }

}

监控线程池:

代码语言:javascript
复制
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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档