二者都是每隔多长时间执行一次任务,那么有什么区别呢?
- scheduleAtFixedRate:真正的间隔时间,是由任务的执行时间和设置的间隔时间长短来决定的,谁长取谁。也就是说,以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。
- scheduleWithFixedDelay:真正的间隔时间,是任务的执行时间加上设置的间隔时间。也就是说,是以上一个任务结束时开始计时,period时间过去后,立即执行。
package timers;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduleExecutorServiceTest
{
public static void main(String[] args) {
ScheduleExecutorServiceTest test = new ScheduleExecutorServiceTest();
test.testWithFixedDelay();
// test.testAtFixedRate();
}
private ScheduledExecutorService executor;
public ScheduleExecutorServiceTest() {
executor = Executors.newScheduledThreadPool(4);
}
/**
* 执行时间和间隔时间,谁长取谁。
* @description
* @author tangcy
* @datetime 2019年5月31日 下午2:10:31
*
* 2019-05-31 14:16:09线程:pool-1-thread-1:Sleeping158ms 执行时间需要 0.158s,少于5秒,则过5秒执行一次
* 2019-05-31 14:16:14线程:pool-1-thread-1:Sleeping2643ms 执行时间需要 2.643s,少于5秒,则过5秒执行一次
* 2019-05-31 14:16:19线程:pool-1-thread-2:Sleeping7613ms 执行时间需要 7.613s,大于5秒,则过7秒执行一次
* 2019-05-31 14:16:26线程:pool-1-thread-1:Sleeping8256ms
* 2019-05-31 14:16:35线程:pool-1-thread-1:Sleeping8565ms
* 2019-05-31 14:16:43线程:pool-1-thread-1:Sleeping9853ms
*
* 通过观察得到,执行时间和间隔时间,谁长取谁。
*
*/
public void testAtFixedRate() {
executor.scheduleAtFixedRate(new Runnable() {
public void run() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = (long) (Math.random() * 10000);
// 输出线程的名字和使用目标对象及休眠的时间
System.out.println(sf.format(new Date())+"线程:"+Thread.currentThread().getName()+":Sleeping"+time+"ms");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
}
}
}, 0, 5000, TimeUnit.MILLISECONDS);
}
/**
* scheduleWithFixedDelay
* 从字面意义上可以理解为就是以固定延迟(时间)来执行线程任务,
* 每次都要把任务执行完成后再延迟固定时间后再执行下一次。
* @description
* @author tangcy
* @datetime 2019年5月31日 下午2:02:02
*/
public void testWithFixedDelay() {
executor.scheduleWithFixedDelay(new Runnable() {
public void run() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = (long) (Math.random() * 10000);
// 输出线程的名字和使用目标对象及休眠的时间
System.out.println(sf.format(new Date())+"线程:"+Thread.currentThread().getName()+":Sleeping"+time+"ms");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
}
}
}, 0, 3000, TimeUnit.MILLISECONDS);
}
}