您的位置:首页 > Web前端

The difference between scheduleAtFixedRate and scheduleWithFixedDelay in JAVA

2013-04-25 13:13 417 查看
  Recently, due to the project needs, I have to use multithread technology in JAVA.

  Luckly, a helpful multithread technology was created way back in JDK 1.5, the ScheduledExecutorService interface. It was derived by ExecutorService class, that can schedule commands to run after a given delay, or to execute periodically.

  It has two methods that some programmers will confused at.

  A. ScheduledFuture<T> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit)

  B .ScheduledFuture<T> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit)

  According to the methods' names, the former one means the command will run at fixed rate, and the latter one means the total time will be delay + command's own running time. We suppose:

Period and delay are both 4 seconds(we call it Tp), and initialDealy is 0 seconds, which means starts immediately

The command need 3 seconds(We call it T0) to complish.

Both of the two methods started at 1:00:00

  For Task A, the command will run at

  1:00:00 /1:00:04 /1:00:08 /1:00:12 ..(because the command's only needs 3 seconds which is less than 4 seconds, so the time will be Tp+0, 2*Tp+0, 3*Tp+0.. what if it's larger than 4 seconds? aha..)

  For task B, the command will run at

  1:00:00/ 1:00:07 / 1:00:14 / 1:00:21 ...(the delay time refers the time after the command finished .Tp+T0, 2*(Tp+T0), 3*(Tp+T0)....)

  Theory are weak, let's see the code.

package concurrent;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

public class ScheduledPool {
public static void main(String args[]){
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);

final ScheduledFuture beeperHandler = scheduler.scheduleAtFixedRate(
new Task("task 1"),1,4,SECONDS);

final ScheduledFuture beeperHandler2 = scheduler.scheduleWithFixedDelay(
new Task("task 2"),1,4,SECONDS);

scheduler.schedule(new Runnable(){
public void run(){
beeperHandler.cancel(true);
beeperHandler2.cancel(true);
scheduler.shutdown();
}
},30,SECONDS);
}
}

class Task implements Runnable{
private String name;
public Task(String n){
name = n;
}
public void run(){
System.out.println(name +"run.." + new Date());
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}


task 1 run.. Thu Apr 25 11:54:35 CST 2013
task 2 run.. Thu Apr 25 11:54:35 CST 2013
task 1 run.. Thu Apr 25 11:54:39 CST 2013
task 2 run.. Thu Apr 25 11:54:42 CST 2013
task 1 run.. Thu Apr 25 11:54:43 CST 2013
task 1 run.. Thu Apr 25 11:54:47 CST 2013
task 2 run.. Thu Apr 25 11:54:49 CST 2013
task 1 run.. Thu Apr 25 11:54:51 CST 2013
task 1 run.. Thu Apr 25 11:54:55 CST 2013
task 2 run.. Thu Apr 25 11:54:56 CST 2013
We can see

for task 1

the time intervals are always 4 seconds which means it's in fixed rate.

for task 2:

the time intervals are 7 sencods which is 4+3, and the 4 seconds means the delay time.

Back to the question we left before, what if the running time is larger than the period in scheduleAtFixedRate method?

Just change the Task sleep time from 3000 to 5000, and the result will be

task 1 run.. Thu Apr 25 11:59:32 CST 2013
task 2 run.. Thu Apr 25 11:59:32 CST 2013
task 1 run.. Thu Apr 25 11:59:37 CST 2013
task 2 run.. Thu Apr 25 11:59:41 CST 2013
task 1 run.. Thu Apr 25 11:59:42 CST 2013
task 1 run.. Thu Apr 25 11:59:47 CST 2013
task 2 run.. Thu Apr 25 11:59:50 CST 2013
task 1 run.. Thu Apr 25 11:59:52 CST 2013
task 1 run.. Thu Apr 25 11:59:57 CST 2013
task 2 run.. Thu Apr 25 11:59:59 CST 2013

Now we can see

for task 1

the time intervals are always 5 seconds (because this time the running time is larger than the period time).

for task 2:

the time intervals are 9 sencods which is 4+5, and the 4 seconds means the delay time, the same as the previous situation.

Hope you can understand the difference between scheduleAtFixedRate and scheduleWithFixedDelay from this site.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: