A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 曾公亮 黑马帝   /  2011-9-30 10:42  /  5657 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

ScheduledExecutorService接口中有两个方法:
scheduleAtFixedRate()和scheduleWithFixedDelay(),他们到底什么区别?
[ 本帖最后由 曾公亮 于 2011-09-30  12:48 编辑 ]

3 个回复

倒序浏览
黑马网友  发表于 2011-9-30 12:18:49
沙发
ScheduledExecutorService接口
scheduleAtFixedRate():安排所提交的Runnable任务按指定的间隔重复执行
scheduleWithFixedDelay():安排所提交的Runnable任务在每次执行完后,等待delay所指定的时间后重复执行。

评分

参与人数 1技术分 +1 收起 理由
wangfayin + 1

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-9-30 12:53:16
藤椅
什么情况会用到scheduleWithFixedDelay()这个方法啊
回复 使用道具 举报
package com.test.basic.executors;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* 测试scheduleAtFixedRate和scheduleWithFixedDelay的区别
* @author yanan.wang
* 2014.2.25
*
*/
public class ScheduledExecutorServiceTest {

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

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

        public static void main(String[] args) {

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

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

scheduleAtFixedRate比scheduleWithFixedDelay方法执行的速度要快。

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马