【JUC】SpringBoot使用线程池的两种方式 注解和注入

17 篇文章 1 订阅
8 篇文章 0 订阅

一、ThreadPoolTaskExecutor与ThreadPoolExecutor的区别

ThreadPoolExecutor 是JDK自1.5添加的线程池。
ThreadPoolTaskExecutor是Spring的线程池【本文使用的】。
Spring线程池ThreadPoolTaskExecutor其实是对JDK线程池ThreadPoolExecutor的一个封装。

二、编写配置文件ThreadPoolConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {
    /** 核心线程数(默认线程数) */
    private static final int CORE_POOL_SIZE = 20;
    /** 最大线程数 */
    private static final int MAX_POOL_SIZE = 50;
    /** 允许线程空闲时间(单位:默认为秒) */
    private static final int KEEP_ALIVE_TIME = 10;
    /** 缓冲队列大小 */
    private static final int QUEUE_CAPACITY = 200;
    /** 线程池名前缀 */
    private static final String THREAD_NAME_PREFIX = "ThreadPool-";

    @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setKeepAliveSeconds(KEEP_ALIVE_TIME);
        executor.setThreadNamePrefix(THREAD_NAME_PREFIX);

        // 线程池对拒绝任务的处理策略
        // CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }
}

二、编写Controller

import com.ynx.exarejuc.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;

    @RequestMapping("/hello100")
    public void sayHello100() throws InterruptedException {
        for (int i = 1; i <100 ; i++) {
            helloService.hello();
        }
    }
}

三、编写Service

3.1、注解

在使用多线程方法上标注@Async时表明调用的线程池 同时在类上和application类上加上@EnableAsync

通过这个@Async注解的方式去异步调用的

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.Executor;

@EnableAsync
@Service
public class HelloService {
   
    //在使用多线程方法上标注@Async时表明调用的线程池 同时在类上和application类上加上@EnableAsync,如下
    @Async("taskExecutor")
    public void  hello() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "处理任务ing...");
    }
}

访问http://localhost:8080/hello100
在这里插入图片描述

3.1、注入

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.Executor;

@Service
public class HelloService {
    @Resource(name = "taskExecutor")
    private Executor taskExecutor;
    
    public void hello(){
        taskExecutor.execute(()->{
            System.out.println(Thread.currentThread().getName() + "处理任务ing...");
        });
    }
}

访问http://localhost:8080/hello100
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值