-1

I am not able to understand the behavior of method defined with @Async annotation. As per my understanding the method should be executed every 1 second. 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
Nital
  • 5,784
  • 26
  • 103
  • 195

1 Answers1

1

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."

As the first method is not @Async, it is blocking the only thread.

Kristof
  • 249
  • 2
  • 7