scheduleAtFixedRate vs scheduleWithFixedDelay

后端 未结 7 1470
时光说笑
时光说笑 2020-12-04 06:18

What\'s the main difference between scheduleAtFixedRate and scheduleWithFixedDelay methods of ScheduledExecutorService?

scheduler.s         


        
相关标签:
7条回答
  • 2020-12-04 06:19

    The scheduleAtFixedRate() method creates a new task and submits it to the executor every period, regardless of whether or not the previous task finished.

    On the other hand, the scheduleWithFixedDelay() method creates a new task after the previous task has finished.

    0 讨论(0)
  • 2020-12-04 06:26

    There is one catch in scheduleAtFixedRate if first thread is taking too long and not ended in given duration then second conscutive thread will not start once the first task will get finsished and will not imediately get started while the first thread has comepleted their task and gievn duration has been elapsed. JVM Will decide when the next task will get executed .

    I think that will help you to choose method Becuase due to this i got big problem

    0 讨论(0)
  • 2020-12-04 06:30

    Visualize time series of invocation scheduleAtFixedRate method. Next executions will start immediately if the last one takes longer than period. Otherwise, it will start after period time.

    time series of invocation scheduleAtFixedRate method

    Time series of invocation scheduleWithFixedDelay method. Next execution will start after delay time between termination of one execution and the commencement of the next, regardless of its execution time

    time series of invocation scheduleWithFixedDelay method

    Hope can help you

    0 讨论(0)
  • 2020-12-04 06:34

    Let's write a simple program:

    import java.util.concurrent.Executors
    import java.util.concurrent.TimeUnit
    
    var time = 0L
    var start = System.currentTimeMillis()
    val executor = Executors.newScheduledThreadPool(1)
    executor.scheduleWithFixedDelay({
        if (time >= 12_000L) {
            executor.shutdown()
        } else {
            Thread.sleep(2000L)
            val now = System.currentTimeMillis()
            time += now - start
            System.out.println("Total $time delay ${now - start}\n")
            start = now
        }
    }, 0L, 1000L, TimeUnit.MILLISECONDS)
    

    And see the results:

    | scheduleWithFixedDelay |   scheduleAtFixedRate  |
    |:----------------------:|:----------------------:|
    | Total 2001 delay 2001  | Total 2003 delay 2003  |
    | Total 5002 delay 3001  | Total 4004 delay 2001  |
    | Total 8003 delay 3001  | Total 6004 delay 2000  |
    | Total 11003 delay 3000 | Total 8004 delay 2000  |
    | Total 14003 delay 3000 | Total 10005 delay 2001 |
    |          ---           | Total 12005 delay 2000 |
    

    NOTICE the execution time is bigger than waiting

    scheduleWithFixedDelay keeps delay
    scheduleAtFixedRate removes delay

    0 讨论(0)
  • 2020-12-04 06:40
    scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("runnable start"); try { Thread.sleep(5000);  System.out.println("runnable end");} catch
         (InterruptedException e) { // TODO Auto-generated catch block
          e.printStackTrace(); }}, 2, 7, TimeUnit.SECONDS);
    
    
    
         scheduledExecutorService.scheduleWithFixedDelay(() -> {
         System.out.println("runnable start"); try { Thread.sleep(5000); System.out.println("runnable end");} catch
         (InterruptedException e) { // TODO Auto-generated catch block
         e.printStackTrace(); } }, 2, 7, TimeUnit.SECONDS);
    

    Just execute it, and you will know the difference. Thank you

    0 讨论(0)
  • 2020-12-04 06:44

    Try adding a Thread.sleep(1000); call within your run() method... Basically it's the difference between scheduling something based on when the previous execution ends and when it (logically) starts.

    For example, suppose I schedule an alarm to go off with a fixed rate of once an hour, and every time it goes off, I have a cup of coffee, which takes 10 minutes. Suppose that starts at midnight, I'd have:

    00:00: Start making coffee
    00:10: Finish making coffee
    01:00: Start making coffee
    01:10: Finish making coffee
    02:00: Start making coffee
    02:10: Finish making coffee
    

    If I schedule with a fixed delay of one hour, I'd have:

    00:00: Start making coffee
    00:10: Finish making coffee
    01:10: Start making coffee
    01:20: Finish making coffee
    02:20: Start making coffee
    02:30: Finish making coffee
    

    Which one you want depends on your task.

    0 讨论(0)
提交回复
热议问题