Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

java - What is the default scheduler pool size in spring-boot?

I'm using spring-boot and @Scheduled annotation to execute some tasks.

How can I find out what the default pool size of scheduled tasks is by default in spring-boot?

Reason: the following class does not execute the jobs in parallel, but one after the other. Maybe only a single thread executor is configured by default?

@Service
public class ZipFileTesterAsync {

    @Scheduled(fixedDelay = 60000, initialDelay = 500)
    public void run() throws Exception {
        System.out.println("import 1");
        TimeUnit.MINUTES.sleep(1);
        System.out.println("import 1 finished");
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 1000)
    public void run2() throws Exception {
        System.out.println("import 2");
        TimeUnit.MINUTES.sleep(1);
    }
}

Result: the 2nd job is executed after the first finished.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, all @Scheduled methods share a single thread by default. It is possible to override this behavior by defining a @Configuration such as this:

@Configuration
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(100);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

This example ensures that all @Scheduled methods share a thread pool of size 100.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...