springboot线程池+等待结束+ThreadPoolTaskExecutor

配置类

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.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@EnableAsync    // 启用 Spring 的异步方法执行功能
@Configuration
public class ExecutorConfig {


    // new ThreadPoolTaskExecutor();
    /**
     * 核心线程数量,默认1
     */
    private int corePoolSize = 6;

    /**
     * 最大线程数量,默认Integer.MAX_VALUE;
     */
    private int maxPoolSize = 6;

    /**
     * 空闲线程存活时间
     */
    private int keepAliveSeconds = 60;

    /**
     * 线程阻塞队列容量,默认Integer.MAX_VALUE
     */
    private int queueCapacity = 1;

    /**
     * 是否允许核心线程超时
     */
    private boolean allowCoreThreadTimeOut = false;


    @Bean("asyncExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 配置核心线程数量
        executor.setCorePoolSize(corePoolSize);
        // 配置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        // 配置队列容量
        executor.setQueueCapacity(queueCapacity);
        // 配置空闲线程存活时间
        executor.setKeepAliveSeconds(keepAliveSeconds);

        executor.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);

        // 设置拒绝策略,直接在execute方法的调用线程中运行被拒绝的任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        // 执行初始化
        executor.initialize();
        return executor;
    }
}

Service

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.ui.context.Theme;

import java.util.Random;
import java.util.concurrent.Future;

@Service
@Slf4j
public class AsyncService {

    @Async("asyncExecutor")
    public Future<String> executeAsync(int a) {
        String msg = "500";
        try {
            Thread.sleep(a * 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":我已经输出了");

        return new AsyncResult<String>(msg);
    }
}

Controller

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@Slf4j
@RestController
public class ThreadTest {
    @Autowired
    private AsyncService asyncService;

    // http://localhost:8080/threadTest
    @GetMapping("threadTest")
    public String OJ() {

        Future<String> s1 = asyncService.executeAsync(1);
        Future<String> s2 = asyncService.executeAsync(2);
        Future<String> s3 = asyncService.executeAsync(3);
        Future<String> s4 = asyncService.executeAsync(4);
        Future<String> s5 = asyncService.executeAsync(5);
        Future<String> s6 = asyncService.executeAsync(6);
        Future<String> s7 = asyncService.executeAsync(7);
        while (!(s1.isDone() && s2.isDone() && s3.isDone() && s4.isDone())) {
            try {
                Thread.sleep(1000);
                System.out.println("还没运行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("运行结束");
        return "ssss";
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot ThreadPoolTaskExecutor mybatis-plus是一个使用SpringBoot框架、ThreadPoolTaskExecutor线程池和mybatis-plus数据库操作框架的技术组合。它可以用于高效地批量插入大数量级数据。具体而言,它利用了SpringBoot框架的便捷性和自动配置功能,通过配置ThreadPoolTaskExecutor线程池来实现多线程处理任务,同时结合mybatis-plus框架的数据库操作能力,实现对大量数据的高效插入。这个组合可以提高数据插入的效率,并且方便开发人员进行配置和管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot+ThreadPoolTaskExecutor+mybatis-plus 多线程批量插入大数量级数据](https://blog.csdn.net/qq_44364267/article/details/127109182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [springboot+webmagic+mybatis-plus架构 小说网站爬虫](https://download.csdn.net/download/yyzc2/11268833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [基于springboot+vue+mybatis-plus的校园管理系统](https://download.csdn.net/download/weixin_46130770/87698991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值