Spring boot中的线程池-ThreadPoolTaskExecutor

一、jdk的阻塞队列:

在这里插入图片描述

二、Spring boot工程的有哪些阻塞队列呢?

1、默认注入的ThreadPoolTaskExecutor

视频解说:

线程池篇-springboot项目中的service层里简单注入ThreadPoolTaskExecutor并且使用_哔哩哔哩_bilibili

程序代码:ThreadPoolDemo/ThreadPool00 · xin麒/XinQiUtilsOrDemo - 码云 - 开源中国 (gitee.com)

简单在service层注入的话是这样的:

@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;

同时在这里使用这个线程池:

@Override
public Object springbootThreadPool(Long count) {
    try {
        threadPoolTaskExecutor.execute(() -> {
            try {
                Thread.sleep(1000 * 1);
                log.debug("v me 50");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "nice";
}

以debug方式启动项目来查看一下,发现这里默认使用的阻塞队列是:

在这里插入图片描述

在这里插入图片描述

2、自定义ThreadPoolTaskExecutor

视频解说:

【2】https://www.bilibili.com/video/BV1Qu4y1X7zk

【3】https://www.bilibili.com/video/BV1Cu4y1i7Ae

程序代码:

https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/ThreadPoolDemo/ThreadPool0

验证方式1-通过启动springboot工程,通过debug形式查看:

@Bean("xinTaskExecutor")
public Executor xinTaskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    //设置线程池参数信息
    taskExecutor.setCorePoolSize(10);
    taskExecutor.setMaxPoolSize(50);
    taskExecutor.setQueueCapacity(0);
    taskExecutor.setKeepAliveSeconds(60);
    taskExecutor.setThreadNamePrefix("xinTaskExecutor--");
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
    taskExecutor.setAwaitTerminationSeconds(60);

    //修改拒绝策略为使用当前线程执行
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    //初始化线程池
    taskExecutor.initialize();
    return taskExecutor;
}

验证方式2-main方法创建并初始化:

通过debug形式查看

①查看initialize方法就可以了

public static void main(String[] args) {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    //设置线程池参数信息
    taskExecutor.setCorePoolSize(10);
    taskExecutor.setMaxPoolSize(50);
    taskExecutor.setQueueCapacity(0);
    taskExecutor.setKeepAliveSeconds(60);
    taskExecutor.setThreadNamePrefix("myExecutor--");
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
    taskExecutor.setAwaitTerminationSeconds(10);

    //修改拒绝策略为使用当前线程执行
    taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    //初始化线程池
    taskExecutor.initialize();
}

②可以看到ExecutorConfigurationSupport类里面有这个方法

public void initialize() {
   if (logger.isInfoEnabled()) {
      logger.info("Initializing ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : ""));
   }
   if (!this.threadNamePrefixSet && this.beanName != null) {
      setThreadNamePrefix(this.beanName + "-");
   }
   this.executor = initializeExecutor(this.threadFactory, this.rejectedExecutionHandler);
}

直接看this.executor = initializeExecutor(this.threadFactory, this.rejectedExecutionHandler)

③来到org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

@Override
protected ExecutorService initializeExecutor(
      ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {

   BlockingQueue<Runnable> queue = createQueue(this.queueCapacity);

   ThreadPoolExecutor executor;
   if (this.taskDecorator != null) {
      executor = new ThreadPoolExecutor(
            this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
            queue, threadFactory, rejectedExecutionHandler) {
         @Override
         public void execute(Runnable command) {
            Runnable decorated = taskDecorator.decorate(command);
            if (decorated != command) {
               decoratedTaskMap.put(decorated, command);
            }
            super.execute(decorated);
         }
      };
   }
   else {
      executor = new ThreadPoolExecutor(
            this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
            queue, threadFactory, rejectedExecutionHandler);

   }

   if (this.allowCoreThreadTimeOut) {
      executor.allowCoreThreadTimeOut(true);
   }

   this.threadPoolExecutor = executor;
   return executor;
}

直接看createQueue(this.queueCapacity)即可

org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#createQueue

可以看到要么new LinkedBlockingQueue<>(queueCapacity)要么就new SynchronousQueue<>()

protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
   if (queueCapacity > 0) {
      return new LinkedBlockingQueue<>(queueCapacity);
   }
   else {
      return new SynchronousQueue<>();
   }
}

那么有没有其他阻塞队列可选呢?这个我就没详细去看了,可以自己尝试下找一下有没有其他方式可以的,难道说重写在里面的方法吗?可行性有待验证。如果创建一个继承了org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor类,且重写了createQueue方法的话那么可以考虑下。就这样吧。

三、和jdk的线程池的区别

1、感觉没什么区别,因为ThreadPoolTaskExecutor内使用的线程池本来就是成员变量中的

@Nullable
private ThreadPoolExecutor threadPoolExecutor;

2、springboot的项目里可以通过注解方式来执行方法

只不过指定使用哪个线程池来执行要异步执行方法的内容。

https://www.bilibili.com/video/BV1A14y1B78x/

如果是默认的注解来执行内容则可能有其他问题:

https://www.bilibili.com/video/BV1Gu4y1q7TY

但是可以通过注解指定使用哪个线程池:

https://www.bilibili.com/video/BV1e44y1c7uE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,你可以使用Java线程池来管理和复用线程。Spring Boot提供了`ThreadPoolTaskExecutor`类,可以用来创建和配置线程池。 首先,在你的Spring Boot应用程序的配置类或配置文件,添加一个`ThreadPoolTaskExecutor`的Bean定义。例如,在Java配置类可以这样配置: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); // 设置核心线程池大小 executor.setMaxPoolSize(20); // 设置最大线程池大小 executor.setQueueCapacity(100); // 设置队列容量 executor.setThreadNamePrefix("MyThread-"); // 设置线程名前缀 executor.initialize(); // 初始化线程池 return executor; } } ``` 在上述配置,我们创建了一个名为`threadPoolTaskExecutor`的线程池Bean,设置了核心线程池大小、最大线程池大小、队列容量和线程名前缀,并初始化了线程池。 接下来,在你的应用程序,可以通过使用`@Async`注解将方法标记为异步执行,并指定使用上述配置的线程池执行方法。例如: ```java import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class MyService { @Async("threadPoolTaskExecutor") public void asyncMethod() { // 异步执行的逻辑 } } ``` 在上述示例,`asyncMethod()`方法被标记为异步执行,并指定了使用名为`threadPoolTaskExecutor`的线程池来执行。 这样,你就可以在Spring Boot应用程序使用线程池来管理和执行异步任务了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值