Springboot使用线程池

之前只是看过线程池的资料,并没有在实际中使用过线程池,今天拿个demo来试下手,理解一下线程池的使用流程:

1.大体的项目结构
在这里插入图片描述
2.线程池配置类

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 创建线程池
 */
@EnableAsync
@Configuration
public class TaskExecutePoolConfig {

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();
        // 核心线程数
        poolExecutor.setCorePoolSize(5);
        // 最大线程数
        poolExecutor.setMaxPoolSize(15);
        // 队列大小
        poolExecutor.setQueueCapacity(100);
        // 线程最大空闲时间
        poolExecutor.setKeepAliveSeconds(300);
        // 拒绝策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程名称前缀
        poolExecutor.setThreadNamePrefix("my-pool-");

        return poolExecutor;
    }
}

3. service层接口

public interface TaskExcuteService {
     void testExecutor(String str);
}

4.实现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class TaskExcuteServiceImpl implements TaskExcuteService{

    @Qualifier("taskExecutor")
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Override
    public void testExecutor(String str) {
        for (int i = 0; i < 10; i++) {
            taskExecutor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "--" + str);
                }
            });
        }

    }
}

5.再写个接口调用实现

import com.example.threadpool.service.TaskExcuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class test{

    @Autowired
    private TaskExcuteService service;

    @RequestMapping("/hello")
    public void testExecutor(final String str) {
        service.testExecutor("aaa");
    }
}

6.看结果:
在这里插入图片描述
7.分析
7.1 配置的核心线程数是5,循环提交了10个任务,中间睡3秒,所以每次会打印5条消息,2次打印完.
在这里插入图片描述在这里插入图片描述
7.2 Service层导入的线程池实现,对应的Config里面的bean类名
在这里插入图片描述
在这里插入图片描述
3.excute 方法,我理解只是向线程池里面提交任务,等待线程池来执行
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值