美文网首页
ScheduledThreadPoolExecutor使用

ScheduledThreadPoolExecutor使用

作者: jsjack_wang | 来源:发表于2018-01-23 16:28 被阅读0次
1.简介
1.1 继承结构
java.lang.Object
  java.util.concurrent.AbstractExecutorService
      java.util.concurrent.ThreadPoolExecutor
          java.util.concurrent.ScheduledThreadPoolExecutor
1.2 实现接口
Executor, ExecutorService, ScheduledExecutorService 
2.常用API
void execute(Runnable command)
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)  
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)  
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)  
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
void shutdown()  

scheduleAtFixedRate与scheduleWithFixedDelay区别:
    scheduleAtFixedRate 按定时任务传入时间严格周期执行
    scheduleWithFixedDelay 按任务具体执行时间来周期执行
3.小栗子
3.1 void execute(Runnable command)
public static void main(String[] args) {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.execute(() -> {
        System.out.println("execute");
    });
    executor.shutdown();
}
3.2 <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
public static void main(String[] args) {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            System.out.println("schedule Callable");
            return null;
        }
    }, 2, TimeUnit.SECONDS);
    executor.shutdown();
}   
3.3 ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
public static void main(String[] args) {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(() -> {
        System.out.println("schedule Runnable");
    }, 2, TimeUnit.SECONDS);
    executor.shutdown();
}
3.4 ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
public static void main(String[] args) {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    long startTime = System.currentTimeMillis() / 1000;
    executor.scheduleAtFixedRate(()->{
        try {
            long endTime = System.currentTimeMillis() / 1000;
            System.out.println("time:" + (endTime - startTime));
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, 1, 2, TimeUnit.SECONDS);
}
result:
    time:1
    time:3
    time:5
    time:7
3.5 ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
public static void main(String[] args) {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    long startTime = System.currentTimeMillis() / 1000;
    executor.scheduleWithFixedDelay(()->{
        try {
            long endTime = System.currentTimeMillis() / 1000;
            System.out.println("time:" + (endTime - startTime));
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, 1, 2, TimeUnit.SECONDS);
}
result:
    time:1
    time:4
    time:7
    time:10
4 归纳
在讲ThreadPoolExecutor时候,我们讲尽量不要直接实例化,而是使用Executors获取连接池对象。ScheduledThreadPoolExecutor是ThreadPoolExecutor子类,我们也建议不要直接实例化,工具类中有获取实例的方法。
Executors API:
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 
    ...
5 总结
scheduleAtFixedRate和scheduleWithFixedDelay虽然delay一样,但是执行之间不一样,具体自己体会,其他的API就不做介绍了,个人感觉还是要多看源码。

相关文章

网友评论

      本文标题:ScheduledThreadPoolExecutor使用

      本文链接:https://www.haomeiwen.com/subject/whgsaxtx.html