Spring boot线程池、@Async、@Async控制个数、@Async线程池一直增加
import org.springframework.context.annotation.Bean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
@Slf4j
@Component
public class UdpQueueServer{
@Bean("taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
executor.setPoolSize(100);
executor.setThreadNamePrefix("test-");
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(10);
return executor;
}
@Async("taskExecutor")
@Scheduled(fixedDelay = 3000)
public void consumerMessage() {
boolean isRunning = true;
while (isRunning) {
try {
Thread.sleep(1000);
log.info("test");
} catch (Exception e) {
e.printStackTrace();
log.info("{}",e);
}
}
}
}