First say Schedulewithfixeddelay (),
Schedulewithfixeddelay is literally understood to be a fixed delay (time) to perform the thread task, which is actually the execution time of the pipeline task, each time after the task has been completed and then delayed a fixed time before the next time.
And Schedulefixedrate, is to perform a thread task at a fixed frequency, the meaning of fixed frequency is that may set a fixed time is not enough to complete the thread task, but it does not matter, to achieve the set delay time will be executed next time.
I don't know if you understand, but here's an example:
public static void Schedulewithfixeddelay () {
final scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (2);
Bell thread
final Runnable beeper = new Runnable () {public
void run () {
SimpleDateFormat SF = new Simpledateforma T ("Yyyy-mm-dd HH:mm:ss");
Long time = (long) (Math.random () * 1000);
The name of the output thread and the time System.out.println to use the target object and Hibernate
(Sf.format (New Date ()) + Thread: +thread.currentthread (). GetName () + ": Sleeping "+time+" "MS");
try {
thread.sleep (time);
} catch (Interruptedexception e) {
}
}
};
Set execution thread schedule, initial 10s delay, delay 10s after each task is completed and perform another task
final scheduledfuture<?> sfuture= Scheduledexecutorservice.schedulewithfixeddelay (beeper,10,10,timeunit.seconds);
Cancel Thread Task
scheduledexecutorservice.schedule (New Runnable () {public
void run () {sfuture.cancel) after 40s
(true );
Scheduledexecutorservice.shutdown ();
}
, timeunit.seconds);
Execution results:
2013-10-16 10:45:51 Thread: pool-1-thread-2:sleeping 726ms
2013-10-16 10:46:02 Thread: pool-1-thread-2:sleeping 288ms
2013-10-16 10:46:12 Thread: pool-1-thread-2:sleeping 294ms
Only 3 executions were performed from the number of execution results, because each time the task was performed and then the next time, the 40s 10s delay was insufficient to perform 4 times.
public static void Scheduleatfixedrate () {//Declaration thread pool final Scheduledexecutorservice Scheduledexecutorservice = executors.
Newscheduledthreadpool (1); The bell thread final Runnable beeper = new Runnable () {public void run () {SimpleDateFormat SF = new SimpleDateFormat ("yyy
Y-mm-dd HH:mm:ss ");
Long time = (long) (Math.random () * 1000); The name of the output thread and the time System.out.println to use the target object and Hibernate (Sf.format (New Date ()) + "Thread:" + thread.currentthread (). GetName () + ": Sleepi
Ng "+ Time +" MS ");
try {thread.sleep (time);
The catch (Interruptedexception e) {}}}; Plan the bell, the initial delay of 10s then with the 10s frequency to perform the bell final scheduledfuture<?> Beeperhandle =
Scheduledexecutorservice.scheduleatfixedrate (Beeper, ten, timeunit.seconds); Cancel the bell and close the thread final Runnable cancelbeeper = new Runnable () {public void run () {System.out.println (thread.currentthr
EAD (). GetName () + "Cancel ...");
Beeperhandle.cancel (TRUE);
Scheduledexecutorservice.shutdown ();
}
}; 60s after execution scheduleatfixedrate ScheduledeXecutorservice.schedule (Cancelbeeper, timeunit.seconds); }
Execution results:
2013-10-16 10:16:50 Thread: pool-1-thread-1:sleeping 868ms
2013-10-16 10:17:00 Thread: pool-1-thread-1:sleeping 587ms
2013-10-16 10:17:10 Thread: pool-1-thread-1:sleeping 313ms
2013-10-16 10:17:20 Thread: pool-1-thread-1:sleeping 969ms
Pool-1-thread-1cancel ...
Look at the time we can know is every 10s to execute the next time.