线程池-使用实例

直接上代码:

线程池相关配置:这些数据可以根据自己业务需要进行调整

#threadpool
#核心线程数
threadpool.corePoolSize=8
#最大线程数
threadpool.maxPoolSize=200
#队列容量
threadpool.queueCapacity=9999
#线程活跃时间(秒)
threadpool.keepAliveSeconds=300

1、线程池配置实体

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * Description: 线程池配置
 *
 * @author w.y.s
 * @date 2020/8/17 15:06
 */
@Configuration
@ConfigurationProperties(prefix = "threadpool")
public class ThreadPoolConfig {

    /**
     * 核心线程数
     */
    private Integer corePoolSize;

    /**
     * 最大线程数
     */
    private Integer maxPoolSize;

    /**
     * 队列容量
     */
    private Integer queueCapacity;

    /**
     * 线程活跃时间(秒)
     */
    private Integer keepAliveSeconds;

    public Integer getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(Integer corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public Integer getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(Integer maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public Integer getQueueCapacity() {
        return queueCapacity;
    }

    public void setQueueCapacity(Integer queueCapacity) {
        this.queueCapacity = queueCapacity;
    }

    public Integer getKeepAliveSeconds() {
        return keepAliveSeconds;
    }

    public void setKeepAliveSeconds(Integer keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
    }
}

2、线程池创建管理

/**
 * Description: 线程池配置
 *
 * @author w.y.s
 * @date 2020/8/17 13:31
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadPooTasklExecutor {

    @Autowired
    ThreadPoolConfig  threadPoolConfig;

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        executor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
        executor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
        executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
        // 设置默认线程名称
        executor.setThreadNamePrefix("thread-");
        // 设置拒绝策略rejection-policy:当pool已经达到max size的时候,如何处理新任务 CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }



}

3、测试使用线程池实例:

  // 1)注入线程池

/**
 * 线程池 taskExecutor
 */
@Resource(name = "taskExecutor")
TaskExecutor taskExecutor;

//2)使用线程池

private void test() {
             // 执行线程
             taskExecutor.execute(new TestThread("test", contest, testService));
   
}

4、线程实例

public class TestThread implements Runnable {

  
    private String title;
    private String context;
   
    @Autowired
    private TestService testService;

    public TestThread (String title, String context, TestService testService) {
        this.subject = title;
        this.context = context;
        this.TestService= testService;
    }

    @Override
    public void run() {

     // 具体的业务逻辑实现
        testService.sendMessage(message);
    }
}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值