Use SpringBoot thread pool

The first step, configure the thread pool

package com.kyy.springboot.pool;

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Auth: zhouhongliang
 * Date:2019/8/1
 */
@Configuration
public class GlobalConfig {
    @Bean
    public ThreadPoolTaskExecutor defaultThreadPool(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数量
        threadPoolTaskExecutor.setCorePoolSize(2);
        //最大线程数量
        threadPoolTaskExecutor.setMaxPoolSize(5);
        //队列中最大任务数
        threadPoolTaskExecutor.setQueueCapacity(2);
        //线程名称前缀
        threadPoolTaskExecutor.setThreadNamePrefix("ThreadPool-");
        //当达到最大线程数时如何处理新任务
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //线程空闲后最大存活时间
        threadPoolTaskExecutor.setKeepAliveSeconds(60);
        //初始化线程池
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}

Next, create Service

package com.kyy.springboot.service;

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

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Auth: zhouhongliang
 * Date:2019/8/1
 */
@Service
public class BootService {
    @Resource(name = "defaultThreadPool")
    private ThreadPoolTaskExecutor poolTaskExecutor;

    @Async
    public void testPool() {
        System.out.println("线程名称:" + Thread.currentThread().getName());
    }

    public void testNoPool() {
        System.out.println("线程名称:" + Thread.currentThread().getName());
    }

    public int testPoolTaskExecutor(int n) throws InterruptedException, ExecutionException {
        CountDownLatch countDownLatch = new CountDownLatch(n);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            final int index = i;
            final Future<Integer> future = poolTaskExecutor.submit(() -> {
                Thread.sleep(5000);
                System.out.println(simpleDateFormat.format(new Date())+" "+Thread.currentThread().getName() + " 执行 " + index);
                countDownLatch.countDown();
                return 1;
            });
        }
        countDownLatch.await();
        return sum;
    }
}

Step 3: Create Controller

package com.kyy.springboot.controller;

import com.kyy.springboot.service.BootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

/**
 * Auth: zhouhongliang
 * Date:2019/8/1
 */
@RestController
public class PoolController {
    @Autowired
    private BootService bootService;
    @RequestMapping("/pool")
    public String pool(){
        for (int i=0;i<100;i++){
            bootService.testPool();
        }
        return "pool test";
    }
    @RequestMapping("/poolTask/{n}")
    public String poolTask(@PathVariable int n){
        long startTime = System.currentTimeMillis();
        try {
            bootService.testPoolTaskExecutor(n);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        return "poolTask test "+(endTime-startTime)/1000+" 秒";
    }
}

Step Four: Create a startup class

package com.kyy.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 启动程序
 * @Auther:zhouhongliang
 * @Date:2019/7/30
 * @Description:
 */
@SpringBootApplication
@EnableAsync
public class SpringBootDemo {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo.class,args);
    }
}

Summary: thread pool configuration instructions
1, Properties Field Description

corePoolSize: thread pool to maintain the minimum number of threads

keepAliveSeconds: allowed free time

maxPoolSize: thread pool to maintain the maximum number of threads

queueCapacity: buffer queue

rejectedExecutionHandler: treatment strategy of denial task

2, (Runable) method execution execute
If at this time the number of threads in the pool is less than corePoolSize, even if the threads in the pool are idle, but also create a new thread to handle the tasks are added.

If at this time is equal to the number of threads in the pool corePoolSize, but workQueue buffer queue is not full, the task is placed in the buffer queue.

If this time is greater than the number of threads in the pool corePoolSize, workQueue buffer queue is full, and the number of threads in the pool is less than the maxPoolSize, build a new thread to process the task is added.

If this time is greater than the number of threads in the pool corePoolSize, workQueue buffer queue is full, and the number of threads in the pool is equal to the maxPoolSize, then processed by the task handler specified policy. That is: the priority processing tasks are: core thread corePoolSize, task queue workQueue, maximum thread maximumPoolSize, if all three are full, use handler processing task rejected.

When the number of threads in the pool is greater than corePoolSize, if the idle time exceeds a thread keepAliveTime, the thread is terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.

Guess you like

Origin blog.51cto.com/11147669/2425799