scheduleAtFixedRate与scheduleWithFixedDelay区别?


scheduleAtFixedRate与scheduleWithFixedDelay区别,代码如下: 

import java.util.Date; 
import java.util.Random; 
import java.util.concurrent.Executors; 
import java.util.concurrent.TimeUnit; 

public class SchedulePool { 

public static void main(String[] args) { 

Task task = new Task(); 

/** 
scheduleAtFixedRate固定频率执行某项任务 
* */ 
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(task, 1000, 1000*2, TimeUnit.MILLISECONDS); 

/** 
scheduleWithFixedDelay相对延迟,固定频率执行某项任务 
* */ 
Executors.newScheduledThreadPool(3).scheduleWithFixedDelay(task, 1000, 1000*2, TimeUnit.MILLISECONDS); 

/** 
* scheduleAtFixedRate和scheduleWithFixedDelay区别。 
* scheduleAtFixedRate方法按照固定的频率执行线程,scheduleWithFixedDelay方法不是按照固定频率执行线程。 
* 该方法是(固定的频率+线程执行需要的时间),方才执行下次线程。 
* */
 


class Task implements Runnable 


@Override 
public void run() { 
System.out.println("当前线程:"+Thread.currentThread()+",执行时间:"+new Date().getTime()); 
try { 
Thread.sleep(new Random().nextInt(1000)); 
} catch (InterruptedException e) { 
e.printStackTrace(); 

伦理片 http://www.dotdy.com/

scheduleAtFixedRate方法执行的结果 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458587192 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458589189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458591189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458593189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458595189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458597189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458599189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458601189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458603189 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458605190 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444458607190 



scheduleWithFixedDelay方法执行的结果 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459052266 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459054629 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459056671 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459059590 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459062251 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459064754 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459067656 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459069756 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459072734 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459075007 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459077357 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459080252 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459082861 
当前线程:Thread[pool-1-thread-1,5,main],执行时间:1444459085492 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值