14 days ago

如何在 springboot 中使用 threadPool

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableAsync
@SpringBootApplication
public class ClearApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClearApplication.class, args);
    }

    @Bean(name = "threadPoolTaskExecutor")
    public Executor threadPoolTaskExecutor() {
//        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

//        taskExecutor.setQueueCapacity(10000);

//        taskExecutor.setCorePoolSize(5);

//        taskExecutor.setMaxPoolSize(100);

//        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

//        taskExecutor.initialize();


        ThreadPoolExecutor threadpool = new ThreadPoolExecutor(
                10,
                20,
                50,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue(100),
                new ThreadPoolExecutor.CallerRunsPolicy());

        ThreadPoolMonitor monitor = new ThreadPoolMonitor(threadpool, 10);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        return threadpool;
    }
}

如果在測試的話,挺需要一個監控來看效果

import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolMonitor implements Runnable{
    private ThreadPoolExecutor executor;
    private int seconds;
    private boolean run = true;

    public ThreadPoolMonitor(ThreadPoolExecutor executor, int delay) {
        this.executor = executor;
        this.seconds = delay;
    }

    public void shutdown() {
        this.run = false;
    }

    public void run() {
        while (run) {
            System.out
                    .println(String
                            .format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s, Queue.size: %d",
                                    this.executor.getPoolSize(), this.executor.getCorePoolSize(),
                                    this.executor.getActiveCount(), this.executor.getCompletedTaskCount(),
                                    this.executor.getTaskCount(), this.executor.isShutdown(),
                                    this.executor.isTerminated(), this.executor.getQueue().size()));

            try {
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

可以用在自己的功能上

 @Async("threadPoolTaskExecutor")
 public void update(UserShopDto userShopDto) {
   //......

 }

也可以用在 spring data

@Repository
public interface UserRepAsync extends PagingAndSortingRepository<User, Long> {
    @Async("threadPoolTaskExecutor")
    CompletableFuture<List<User>> findByCardNo(String cardNo);
}
← Spring Boot jar exclude source Springboot 效能調整紀錄 →
 
comments powered by Disqus