お待ちください...

小波Note

四川 · 成都市13 ℃
日本語

Spring bootスレッドプール

成都 (cheng du)2024/8/28 11:36:161.26k見積もり読書時間 4 分お気に入りCtrl + D / ⌘ + D
cover
IT FB(up 主)
バックエンド開発エンジニア
AsyncConfig.java
        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;

@Configuration
@EnableAsync
public class AsyncConfig {
    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("MyExecutor-");
        executor.initialize();
        return executor;
    }
}

    
java
        import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Autowired
    private Executor taskExecutor;

    @Async("taskExecutor")
    public void asyncMethod() {
        System.out.println("Executing method asynchronously - " + Thread.currentThread().getName());
    }
}