`

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!
分享到:
评论
1 楼 senninha 2017-08-29  
这个。。是api说明吧。。

相关推荐

Global site tag (gtag.js) - Google Analytics