【Java并发核心七】计划任务ScheduleExecutorService

Java中定时任务Timer工具类提供了计划任务的实现,但是Timer工具类是以队列的方式来管理线程的,并不是以线程池的方式,这样在高并发的情况下,运行效率会有点低。

ScheduleExecutorService 主要作用是将定时任务与线程池结合使用。

ScheduleExecutorService 的父接口是Executor,父类是ThreadPoolExecutor。

看个例子

     final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
        List<Future<String>> futureList = new ArrayList<Future<String>>();

        // 此线程池运行5个线程
        for (int i = 0; i < 5; i++) {
            final int index = i;
            System.out.println("Thread-" + index + "-add-" + sf.format(new Date()));
            Future<String> future = executor.schedule(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("Thread-" + index + "-begin-" + sf.format(new Date()));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread-" + index + "-end-" + sf.format(new Date()));
                    return "index-" + index;
                }

            }, 4L, TimeUnit.SECONDS); // 延迟4秒后执行
            futureList.add(future);
            System.out.println("Thread-" + index + "-add over-" + sf.format(new Date()));
        }

        // future.get() 是阻塞执行的,所以获取值要在线程都启动之后,再获取
        for (Future<String> future : futureList) {
            try {
                System.out.println(future.get()); // 获取线程返回值
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

《【Java并发核心七】计划任务ScheduleExecutorService》

使用工厂类产生ScheduleExecutorService有两个api:Executors.newScheduledThreadPool( int )  和 Executors.newSingleThreadScheduledExecutor() 。

ScheduleExecutorService 可以运行Callable,也可以运行Runnable,常用api如下:

execute(Runnable command)    直接执行,执行命令所需的延迟为零

 getQueue() 返回 BlockingQueue<Runnable>    返回此执行器使用的任务队列

schedule(Callable<V> callable, long delay, TimeUnit unit)    创建并执行给定延迟后启用的计划任务

schedule(Runnable command, long delay, TimeUnit unit)    创建并执行给定延迟后启用的计划任务

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)    创建并执行一个周期性操作,该操作首先在给定的初始延迟之后启用,然后以给定的周期启用

scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)    创建并执行周期性动作,主要左右那个是设置多个任务之间固定的运行时间间隔。

shutdown()    启动一个有序的关闭,其中先前提交的任务被执行,但是没有新任务被接受

shutdownNow()    尝试停止所有积极执行任务,停止等待任务的处理,并返回等待执行的任务的列表

submit(Callable<T> task)    提交一个返回值的任务用于执行

submit(Runnable task)    提交可执行任务以执行

submit(Runnable task, T result)    提交可执行任务以执行

    原文作者:快乐菠菜
    原文地址: https://www.cnblogs.com/klbc/p/9797219.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞