[英]Why is the @Async task in Spring Boot scheduler not running every 1 second
I am not able to understand the behavior of method defined with @Async
annotation.我无法理解使用@Async
注释定义的方法的行为。 As per my understanding the method should be executed every 1 second.据我了解,该方法应每 1 秒执行一次。 But I see otherwise.但我认为并非如此。
@EnableAsync
@Component
public class AsyncSchedulingDemo {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//every 1 second, INSTANT start of job but waits for the first job to finish hence takes 10 seconds
//here the next execution will start after 10 seconds because the job takes 10 seconds to finish
@Scheduled(fixedRate = 1000)
public void performFixedRateTask_W_LongRunningJob() throws InterruptedException {
System.out.println("Task performed every 1 second but waits for the first one to finish before the next execution starts " + dateFormat.format(new Date()));
Thread.sleep(10000);
}
//should be executed every 1 second because of @Async
//here the next execution should start IMMEDIATELY and does NOT wait for the first one to finish
@Async
@Scheduled(fixedRate = 1000)
public void performFixedRateTask_With_Async() throws InterruptedException {
System.out.println("Async Task performed every 1 second as it does NOT wait for the first one to finish " + dateFormat.format(new Date()));
Thread.sleep(10000);
}
}
Console Log:控制台日志:
Task performed every 1 second but waits for the first one to finish before the next execution starts 05/13/2020 15:54:51
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 15:54:51
Task performed every 1 second but waits for the first one to finish before the next execution starts 05/13/2020 15:55:01
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 15:55:01
Task performed every 1 second but waits for the first one to finish before the next execution starts 05/13/2020 15:55:11
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 15:55:11
===UPDATED=== ===更新===
If I comment out the very first method completely and execute the application again I see that the second method executes every second.如果我完全注释掉第一个方法并再次执行应用程序,我会看到第二个方法每秒执行一次。 Why??为什么?? How is the first method preventing the execution of second one?第一种方法如何防止执行第二种方法?
Console Log:控制台日志:
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 16:45:48
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 16:45:49
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 16:45:50
Async Task performed every 1 second as it does NOT wait for the first one to finish 05/13/2020 16:45:51
I think, it is because the default pool-size for threads used for executing @Scheduled
annotated methods is 1 according to spring documentation : "If you do not provide a 'pool-size' attribute, the default thread pool will only have a single thread."我认为,这是因为根据spring 文档,用于执行@Scheduled
注释方法的线程的默认池大小为 1:“如果您不提供 'pool-size' 属性,则默认线程池将只有一个线。”
As the first method is not @Async
, it is blocking the only thread.由于第一种方法不是@Async
,它阻塞了唯一的线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.