scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

package thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class Test03 {

	private static final AtomicLong l = new AtomicLong(0) ;

	private static final ScheduledExecutorService scheduler = 
		Executors.newScheduledThreadPool(10);
	

	public static void main(String[] args) {

		/* 
		 * 使用scheduleAtFixedRate , 该方法第三个参数表示在上一个个任务开始执行之后延迟
		 * 多少秒之后再执行, 是从上一个任务开始时开始计算
		 * 但是还是会等上一个任务执行完之后,下一个任务才开始执行,最后的结果,就是感觉延迟失去
		 * 了作用
		 *  */
		ScheduledFuture<?> sf1 = scheduler.scheduleAtFixedRate(new Runnable() {
			public void run() {
				long i =  l.getAndAdd(1) ;
				System.out.println("start " + i);
				try {
					TimeUnit.SECONDS.sleep(5) ;
				} catch (InterruptedException e) {  
					e.printStackTrace();
				}
				System.out.println("end " + i);
			}
		}, 0, 2 , TimeUnit.SECONDS) ;

		/* 
		 * 使用scheduleWithFixedDelay该方法第三个参数表示在上一个个任务结束执行之后延迟
		 * 多少秒之后再执行, 是从上一个任务结束时开始计算
		 *  */
		ScheduledFuture<?> sf2 = scheduler.scheduleWithFixedDelay(new Runnable() {
			public void run() {
				long i =  l.getAndAdd(1) ;
				System.out.println("start " + i);
				try {
					TimeUnit.SECONDS.sleep(3) ;
				} catch (InterruptedException e) {  
					e.printStackTrace();
				}
				System.out.println("end " + i);
			}
		}, 0, 2, TimeUnit.SECONDS) ;



	}

}
<span style="background-color: rgb(255, 255, 255);">//------------</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">                                                      </span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">自己看到这样子的结果也觉得比较奇怪,这个scheduleAtFixedRate()方法按照理解应该是直接每个2秒中执行一次。</span></span><pre name="code" class="java" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; background-color: rgb(245, 245, 245);"> </span><span style="box-sizing: content-box; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; color: rgb(255, 0, 0); background-color: rgb(245, 245, 245);">If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.</span></span>
<span style="font-family:Helvetica, Tahoma, Arial, sans-serif;color:#ff0000;"><span style="line-height: 24px; background-color: rgb(245, 245, 245);">这个函数的英文文档的一句话已经说明了,这个方法执行的时间可能会超过执行频率的周期,那么后面的任务会延期执行了。</span></span>
 
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值