there are t wo methods of ScheduledExecutorService Class, one is scheduleAtFixedRate, the other is scheduleWithFixedDelay.
NOTE: for the scheduleAtFixedRate function, when the execution of this task takes longer than its period, then subsequent executions may start late, and only after the expected times be executed, the subsequent runtime would be normal, otherwise, the subsequent time would be immediate time after the last execution finished.
in the following example, the delay is 5000 ms, because the first and the second executions have cost time more than the period, and during this time, three times execution have been postponed, so from the 3 to 5 time execution would be immediately launched after the last execution finished. After that, the 6th execution then resume the normal time, the run time = 14:59:27 + 5 *5000 =14:59:52.
1. 9,beg TIme Fri Aug 10 14:59:27 CST 2012
9,end TIme Fri Aug 10 14:59:35 CST 2012,,,,costTime=7999
2. 9,beg TIme Fri Aug 10 14:59:35 CST 2012
9,end TIme Fri Aug 10 14:59:39 CST 2012,,,,costTime=3992
3. 9,beg TIme Fri Aug 10 14:59:39 CST 2012
9,end TIme Fri Aug 10 14:59:41 CST 2012,,,,costTime=1996
4. 9,beg TIme Fri Aug 10 14:59:42 CST 2012
9,end TIme Fri Aug 10 14:59:44 CST 2012,,,,costTime=1996
5. 9,beg TIme Fri Aug 10 14:59:47 CST 2012
9,end TIme Fri Aug 10 14:59:49 CST 2012,,,,costTime=1996
6. 9,beg TIme Fri Aug 10 14:59:52 CST 2012
9,end TIme Fri Aug 10 14:59:52 CST 2012,,,,costTime=0
7. 9,beg TIme Fri Aug 10 14:59:57 CST 2012
9,end TIme Fri Aug 10 14:59:59 CST 2012,,,,costTime=1995
package main;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TestMain {
static int sleepTime=0;
/**
* @param args
*/
public static void main(String[] args) {
testScheduleWithFixedDelay();
testScheduleAtFixedRate();
}
public static void testScheduleWithFixedDelay() {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
TestMain main=new TestMain();
One one= main.new One();
sleepTime=10000;
ScheduledFuture futur = executorService.scheduleWithFixedDelay(one,
0, 5000,
TimeUnit.MILLISECONDS);
/** Result:
9,beg TIme Fri Aug 10 14:25:30 CST 2012
9,end TIme Fri Aug 10 14:25:40 CST 2012,,,,
9,beg TIme Fri Aug 10 14:25:45 CST 2012
9,end TIme Fri Aug 10 14:25:55 CST 2012,,,,
9,beg TIme Fri Aug 10 14:26:00 CST 2012
9,end TIme Fri Aug 10 14:26:10 CST 2012,,,,
9,beg TIme Fri Aug 10 14:26:15 CST 2012
9,end TIme Fri Aug 10 14:26:25 CST 2012,,,,
*/
}
public static void testScheduleAtFixedRate() {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
TestMain main=new TestMain();
One one= main.new One();
sleepTime=15000;
ScheduledFuture futur = executorService.scheduleAtFixedRate(one,
0, 5000,
TimeUnit.MILLISECONDS);
for(int i=0;i<15;i++){
if(sleepTime>1000){
sleepTime=sleepTime-2000;
}else{
sleepTime=sleepTime+2000;
}
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/** Result
9,beg TIme Fri Aug 10 14:59:27 CST 2012
9,end TIme Fri Aug 10 14:59:35 CST 2012,,,,costTime=7999
9,beg TIme Fri Aug 10 14:59:35 CST 2012
9,end TIme Fri Aug 10 14:59:39 CST 2012,,,,costTime=3992
9,beg TIme Fri Aug 10 14:59:39 CST 2012
9,end TIme Fri Aug 10 14:59:41 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 14:59:42 CST 2012
9,end TIme Fri Aug 10 14:59:44 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 14:59:47 CST 2012
9,end TIme Fri Aug 10 14:59:49 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 14:59:52 CST 2012
9,end TIme Fri Aug 10 14:59:52 CST 2012,,,,costTime=0
9,beg TIme Fri Aug 10 14:59:57 CST 2012
9,end TIme Fri Aug 10 14:59:59 CST 2012,,,,costTime=1995
9,beg TIme Fri Aug 10 15:00:02 CST 2012
9,end TIme Fri Aug 10 15:00:02 CST 2012,,,,costTime=0
9,beg TIme Fri Aug 10 15:00:07 CST 2012
9,end TIme Fri Aug 10 15:00:09 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 15:00:12 CST 2012
9,end TIme Fri Aug 10 15:00:14 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 15:00:17 CST 2012
9,end TIme Fri Aug 10 15:00:19 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 15:00:22 CST 2012
9,end TIme Fri Aug 10 15:00:24 CST 2012,,,,costTime=1996
9,beg TIme Fri Aug 10 15:00:27 CST 2012
9,end TIme Fri Aug 10 15:00:29 CST 2012,,,,costTime=1995
9,beg TIme Fri Aug 10 15:00:32 CST 2012
*/
}
public class One extends Thread{
@Override
public void run() {
long start=System.currentTimeMillis();
System.out.println(Thread.currentThread().getId()+",beg TIme "+new Date());
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println(Thread.currentThread().getId()+",end TIme "+new Date(System.currentTimeMillis())+",,,,costTime="+(end-start));
}
}
}
An ExecutorService
that can schedule commands to run after a given delay, or to execute periodically.
The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. ThescheduleAtFixedRate andscheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.
scheduleAtFixedRate
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
scheduleWithFixedDelay
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.