ScheduledThreadPoolExecutor

public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService {

public interface ScheduledExecutorService extends ExecutorService {

ScheduledExecutorService多了四个延时或周期任务接口

//达到给定的延时时间后,执行任务。这里传入的是实现Runnable接口的任务,
//因此通过ScheduledFuture.get()获取结果为null
public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);
//达到给定的延时时间后,执行任务。这里传入的是实现Callable接口的任务,
//因此,返回的是任务的最终计算结果
 public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

//是以上一个任务开始的时间计时,period时间过去后,
//检测上一个任务是否执行完毕,如果上一个任务执行完毕,
//则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);
//当达到延时时间initialDelay后,任务开始执行。上一个任务执行结束后到下一次
//任务执行,中间延时时间间隔为delay。以这种方式,周期性执行任务。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

使用

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture<?> schedule = pool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
            }
        }, 1, TimeUnit.SECONDS);
        System.out.println(schedule.get());

        ScheduledFuture<Integer> schedule1 = pool.schedule(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("schedule Callable " + new Date());
                return 1;
            }
        }, 2, TimeUnit.SECONDS);
        System.out.println(schedule1.get());

    }

main Wed Sep 04 11:07:42 CST 2019
schedule Runnable Wed Sep 04 11:07:43 CST 2019
null
schedule Callable Wed Sep 04 11:07:45 CST 2019
1

scheduleAtFixedRate使用

延时1秒后,执行第一次任务,周期2秒过后,再次执行任务

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture<?> schedule = pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());
        
    }

main Wed Sep 04 11:14:06 CST 2019
schedule Runnable Wed Sep 04 11:14:07 CST 2019
schedule Runnable Wed Sep 04 11:14:09 CST 2019
schedule Runnable Wed Sep 04 11:14:11 CST 2019

如果任务时间3秒,大于周期时间2秒,等待任务执行完毕后,立即再次执行任务

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture<?> schedule = pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());

    }

main Wed Sep 04 11:16:12 CST 2019
schedule Runnable Wed Sep 04 11:16:13 CST 2019
schedule Runnable Wed Sep 04 11:16:16 CST 2019
schedule Runnable Wed Sep 04 11:16:19 CST 2019

scheduleWithFixedDelay使用

等待任务执行完毕后,延时2秒,再次执行任务

   public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture<?> schedule = pool.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());

    }

main Wed Sep 04 11:18:11 CST 2019
schedule Runnable Wed Sep 04 11:18:12 CST 2019
schedule Runnable Wed Sep 04 11:18:17 CST 2019
schedule Runnable Wed Sep 04 11:18:22 CST 2019

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值