SpringBoot 线程池

SpringBoot 线程池

  • 一、版本相关
  • 二、线程池配置
  • 三、任务
  • 四、触发线程
  • 五、参考鸣谢
  • 六、程序源码

一、版本相关

项目 版本
spring-boot-starter-parent 2.1.6.RELEASE

二、线程池配置

package com.example.thread.configs;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 多线程下载打包配置
 * 配置类实现AsyncConfigurer接口,并重写getAsyncExecutor方法,并返回一个ThreadPoolTaskExecutor,
 * 这样我们就获得一个基于线程池TaskExecutor
 * 参考 https://www.cnblogs.com/yw0219/p/8810956.html
 */
@Configuration
@ComponentScan("com.example.thread.service")
@EnableAsync//利用@EnableAsync注解开启异步任务支持
public class MultiThreadingConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5);
        // 设置最大线程数
        executor.setMaxPoolSize(10);
        // 设置队列容量
        executor.setQueueCapacity(2000);
        // 设置线程活跃时间(秒)
        executor.setKeepAliveSeconds(120);
        // 设置默认线程名称
//        executor.setThreadNamePrefix("hello-");
        // 设置拒绝策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
//        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
    }

}

三、任务

package com.example.thread.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建线程任务服务
 */
@Service
public class MultiThreadService {

    /**
     * 通过@Async注解表明该方法是一个异步方法,
     * 如果注解在类级别上,则表明该类所有的方法都是异步方法,而这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
     */
    @Async
    public void executeAysncTask(){
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String context = "hello " + date;
        System.out.println("Task : " + context);
    }
} 

四、触发线程

package com.example.thread.controller;

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

@RestController
public class ThreadController {

    @Autowired
    MultiThreadService service;

    @RequestMapping("test")
    public String hello(){
        service.executeAysncTask();
        return "hello";
    }
} 

五、参考鸣谢

https://www.cnblogs.com/yw0219/p/8810956.html

六、程序源码

https://github.com/sleetdream/demo

你可能感兴趣的:(Java,SpringBoot)