ScheduledExecutorService 源码分析


public interface ScheduledExecutorService extends ExecutorService {

// 创建在指定延迟后执行且只运行一次的的任务
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);

// 同上,这里是callable对象
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);

// 创建在初始延迟后执行并周期性调用的的任务
// 如果执行任务时间大于周期,则下一个任务开始时间会推迟,不会存在并行执行。
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);

// 创建在初始延迟后执行并周期性调用的的任务。
// 和上面的区别:这里的delay是前一个任务执行完成后,和下一个任务开始时间的间隔。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);

}



测试:

public class ScheduledExecutorServiceTest {

private static final int POOL_SIZE = 4;

private static ScheduledExecutorService ses = null;

@org.junit.Before
public void setUp() {
ses = Executors.newScheduledThreadPool(POOL_SIZE);
}

@After
public void tearDown() {
ses.shutdown();
}

@Test
public void scheduled() throws ExecutionException, InterruptedException {
MyCall task = new MyCall();
System.out.println(System.currentTimeMillis() + " : scheduled...");
// 延时100+1000毫秒打印
ScheduledFuture<String> future = ses.schedule(task, 100, TimeUnit.MILLISECONDS);
String result = future.get();
System.out.println(System.currentTimeMillis() + " result: " + result);
}

@Test
public void scheduledAtFixedRateTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAtFixedRate...");
// 首次延迟100毫秒,然后每隔max(2000,3000)毫秒打印
ses.scheduleAtFixedRate(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}

@Test
public void scheduledAfFixedDelayTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAfFixedDelay...");
// 首次延迟100毫秒,然后每隔5000毫秒打印
ses.scheduleWithFixedDelay(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}

static class MyCall implements Callable<String> {

@Override
public String call() throws Exception {
TimeUnit.MILLISECONDS.sleep(1000);
return "MyTask is done";
}
}

static class MyCmd implements Runnable {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() + " : MyCmd is done!");
}
}
}


scheduled输出 (延时100+1000毫秒打印)
1505538732156 : scheduled...
1505538733257 result : MyTask is done

scheduledAtFixedRateTest输出 (间隔3000毫秒=max(2000, 3000)):
1505538684893 :scheduledAtFixedRate...
1505538687996 : MyCmd is done!
1505538690997 : MyCmd is done!

scheduledAtFixedDelayTest输出 (间隔5000毫秒=2000+3000):
1505536678015 :scheduledAfFixedDelay...
1505536681117 : MyCmd is done!
1505536686118 : MyCmd is done!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值