浅析Java高并发下ScheduleThreadPoolExecutor延时任务

浅析Java高并发下ScheduleThreadPoolExecutor延时任务

Java中的计划任务Timer工具类提供了以计时器或计划任务的功能来实现按指定时间或时间间隔执行任务,但由于Timer工具类并不是以池pool方式实现的,而是以队列的方式来管理线程的,所以在高并发的情况下运行效率较低,在JDK 1.5版本以后提供了ScheduledExecutorService对象来解决效率与定时任务的性能问题。

这篇文章我们主要讨论ScheduledExecutorService的使用技巧以及一些常用的线程池操作方法,后面的文章会继续对执行器进行深入的交流探讨。

image

Executors 工具类提供了两个常用的ScheduledThreadPoolExecutor

这两个常用的ScheduledThreadPoolExecutor:SingleThreadScheduledExecutor(单线程的线程池)、ScheduledThreadPool(线程数量固定的线程池),下面是 Executors 对应的源代码。

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
}

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory arg) {
    return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1, arg));
}

public static ScheduledExecutorService newScheduledThreadPool(int arg) {
    return new ScheduledThreadPoolExecutor(arg);
}

public static ScheduledExecutorService newScheduledThreadPool(int arg, ThreadFactory arg0) {
    return new ScheduledThreadPoolExecutor(arg, arg0);
}

ScheduledExecutorService是一个接口,继承于ExecutorService,支持线程池的所有功能,同时也提供了四个用于计划任务调度的核心方法。

下面我将介绍这四个方法的使用和一些常用的线程池方法:

1、schedule runnable

带延迟时间的调度,只执行一次,返回值为实现Future接口的对象,可调用Future.get()方法阻塞直到任务执行完毕

/**
 * 创建并执行在给定延迟后启用的一次性操作
 *
 * @param command 要执行的任务 
 * @param delay 从现在开始延迟执行的时间 
 * @param unit 延时参数的时间单位 
 * @return 表示任务等待完成,并且其的ScheduledFuture get()方法将返回 null 
 * @throws RejectedExecutionException 如果任务无法安排执行 
 * @throws NullPointerException 如果命令为空 
 */
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);

schedule runnable使用示例

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<?> future = scheduled.schedule(() -> {
        try {
            System.out.println("开始执行任务");
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("执行完毕");
    }, 1000, TimeUnit.MILLISECONDS);
    System.out.println("阻塞开始");
    System.out.println(future.get() + "");
    System.out.println("阻塞结束");
}

执行结果如下:

阻塞开始
开始执行任务
执行完毕
null
阻塞结束

schedule runnable,这个方法是不提供返回值的,所以调用future.get()方法返回的是null

2、schedule callable

带延迟时间的调度,只执行一次,返回值为实现Future接口的对象,调用Future.get()方法阻塞直到任务完成,可以获取到返回结果

/**
 * 创建并执行在给定延迟后启用的ScheduledFuture
 *
 * @param callable 执行的功能 
 * @param delay 从现在开始延迟执行的时间 
 * @param unit 延迟参数的时间单位 
 * @param <V> the 可调用结果的类型 
 * @return一个可用于提取结果或取消的ScheduledFuture 
 * @throws RejectedExecutionException 如果该任务无法安排执行 
 * @throws NullPointerException 如果callable为空 
 */
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);

schedule Callable 使用示例

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<String> future = scheduled.schedule(() -> {
        try {
            System.out.println("开始执行任务");
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("执行完毕");
        return "success";
    }, 1000, TimeUnit.MILLISECONDS);
    System.out.println("阻塞开始");
    System.out.println(future.get() + "");
    System.out.println("阻塞结束");
}

执行结果:

阻塞开始
开始执行任务
执行完毕
success
阻塞结束

schedule callable 是带返回值的,通过future.get()获取

3、scheduleAtFixedRate

创建并执行一个在给定初始延迟后的定期操作,也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后下一个任务执行,接着在 initialDelay + 2 * period 后执行,依此类推 ,也就是只在第一次任务执行时有延时。

/**
 * @param command 要执行的任务 
 * @param initialDelay 首次执行的延迟时间
 * @param period 连续执行之间的周期
 * @param unit initialDelay和period参数的时间单位 
 * @return 一个ScheduledFuture代表待完成的任务,其 get()方法将在取消时抛出异常 
 * @throws RejectedExecutionException 如果任务无法安排执行 
 * @throws NullPointerException 如果命令为空 
 * @throws IllegalArgumentException 如果period小于或等于零 
 */
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

scheduleAtFixedRate使用示例

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(5);
    ScheduledFuture<?> future = scheduled.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            try {
                System.out.println("开始执行任务");
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("执行完毕");
        }
    }, 1000L, 1000L, TimeUnit.MILLISECONDS);
    System.out.println("阻塞开始");
    System.out.println(future.get() + "");
    System.out.println("阻塞结束");    
}

打印结果如下:

阻塞开始
开始执行任务
执行完毕
开始执行任务
执行完毕
开始执行任务
执行完毕
....

4、scheduleWithFixedDelay

创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟,即总时间是(initialDelay + period)* n

/**
 * @param command 要执行的任务 
 * @param initialDelay 首次执行的延迟时间
 * @param delay 一次执行终止和下一次执行开始之间的延迟
 * @param unit initialDelay和delay参数的时间单位
 * @return 表示挂起任务完成的ScheduledFuture,并且其get()方法在取消后将抛出异常
 * @throws RejectedExecutionException 如果任务不能安排执行 
 * @throws NullPointerException 如果command为null
 * @throws IllegalArgumentException 如果delay小于等于0
 */
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);

scheduledWithFixedDelay使用示例

public static void main(String[] args) throws ExecutionException, InterruptedException {
    ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(5);
    ScheduledFuture<?> future = scheduled.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            try {
                System.out.println("开始执行任务");
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("执行完毕");
        }
    }, 1000L, 1000L, TimeUnit.MILLISECONDS);
    System.out.println("阻塞开始");
    System.out.println(future.get() + "");
    System.out.println("阻塞结束");
}

打印结果如下:

阻塞开始
开始执行任务
执行完毕
开始执行任务
执行完毕
开始执行任务
执行完毕
....

scheduleAtFixedRate和scheduleWithFixedDelay的区别在于,scheduleAtFixedRate()为固定频率,scheduleWithFixedDelay()为固定延迟。固定频率是相对于任务执行的开始时间,而固定延迟是相对于任务执行的结束时间,这就是他们最根本的区别!

5、线程池关闭,shutdown()和shutdownNow()的使用

两个关闭线程池的方法,一旦线程池被关闭,就会拒绝以后提交的所有任务

使用shutdown()可以使用awaitTermination等待所有线程执行完毕当前任务。在shutdown以前已提交任务的执行中发起一个有序的关闭,但是不接受新任务。

使用shutdownNow()尝试停止所有正在执行的任务、暂停等待任务的处理,并返回等待执行的任务列表。对于正在运行,尝试通过中断该线程来结束线程。对于尚未运行的任务,则都不再执行。

class PrintThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "PrintThreadFactory");
    }
}
public static void main(String[] args) {
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory());
    Runnable runnable = () -> {
        System.out.println("print " + count.getAndIncrement());
        if (count.get() == 3) {
            countDownLatch.countDown();
            System.out.println("任务继续...");
            try {
                Thread.sleep(3000L);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("任务结束");
        }
    };
    schedule.scheduleAtFixedRate(runnable, 0L, 2L, TimeUnit.SECONDS);
    try {
        countDownLatch.await();
        schedule.shutdown();  //平滑停止线程,不处理新任务,完成正在执行的任务
//      schedule.shutdownNow();  // 尝试强制停止线程,让终止的线程去设置休眠会抛出异常

        if (schedule.isShutdown()) {
            System.out.println("Scheduled is shutdown");
        }
        if (schedule.awaitTermination(10L, TimeUnit.SECONDS)) {
            System.out.println("termination");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

通过Future.cancel()取消运行任务

cancel()方法接收参数是布尔型的,传入true会中断线程停止任务,传入false则会让线程正常执行至完成。这里传入false既然不会中断线程,那么这个cancel方法不就没有意义了?

class PrintThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "PrintThreadFactory");
    }
}
public static void main(String[] args) {
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1, new PrintThreadFactory());
    Runnable runnable = () -> {
        System.out.println("print " + count.getAndIncrement());
        if (count.get() == 3) {
            countDownLatch.countDown();
        }
    };
    Future future = schedule.scheduleAtFixedRate(runnable, 0L, 2L, TimeUnit.SECONDS);
    try {
        countDownLatch.await();
        future.cancel(true);
        if (future.isCancelled()) {
            System.out.println("is Cancelled");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

简单来说传入false参数只能取消还没开始的任务,若任务已经开始了,就由其运行下去。所以对于已经开始的任务,如果想要停止的话,需要给cancel方法的参数设置为true。

6、ScheduledThreadPoolExecutor参数使用

image

continueExistingPeriodicTasksAfterShutdown,对于通过scheduleAtFixedRate、scheduleWithFixedDelay 提交的周期任务有效 默认值为false,设置为true表示当执行器调用shutdown后,继续执行延时任务;

与之对应的get和set方法

void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)
boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()

executeExistingDelayedTasksAfterShutdown,对于通过schedule()方法提交的延时任务有效,默认为true,设置为false表示当执行器调用shutdown后,不再继续执行现有延迟任务;

与之对应的get和set方法

void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)
boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()

removeOnCancel,默认为false,设置为true则从队列中删除执行任务;

与之对应的get和set方法

void setRemoveOnCancelPolicy(boolean value)
boolean getRemoveOnCancelPolicy()

使用示例

public static void main(String[] args) {

        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);

        Runnable runnable = () -> System.out.println(Thread.currentThread().getName() + "_1");

//        ScheduledFuture<?> future = executor.schedule(runnable, 3, TimeUnit.SECONDS);
        // 对于通过schedule()方法提交的延时任务有效,默认为true,设置为false表示当执行器调用shutdown后,不再继续执行现有延迟任务
//        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);

//        System.out.println(executor.getQueue().size());
//        // 默认为false,设置为true则从队列中删除执行任务
//        executor.setRemoveOnCancelPolicy(false);
//        future.cancel(true);
//        System.out.println(executor.getQueue().size());

        executor.scheduleAtFixedRate(runnable, 1L, 1L, TimeUnit.SECONDS);
//        //对于通过scheduleAtFixedRate、scheduleWithFixedDelay 提交的周期任务有效 默认值为false,设置为true表示当执行器调用shutdown后,继续执行延时任务
        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);

        executor.shutdown();

        System.out.println("线程池停止");
    }

7、其他

零延时的 execute()、submit() 方法

execute()、submit() 方法都被重写了,本质上调用的还是 schedule() 方法;从下面的源码可以看出,这两个方法提交的任务都是延时为0的 “实时任务”;

public void execute(Runnable arg0) {
    this.schedule(arg0, 0L, TimeUnit.NANOSECONDS);
}
public Future<?> submit(Runnable arg0) {
    return this.schedule(arg0, 0L, TimeUnit.NANOSECONDS);
}

封装计划任务线程池工具类

下面是使用单例模式封装的工具类

public final class MyScheduledExecutor {

    // 全局用于处理接收Future对象的集合
    private ConcurrentHashMap<String, Future> futureMap = new ConcurrentHashMap<>();

    // 计划执行任务
    private ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(5);

    private MyScheduledExecutor() {
    }

    // 设计为单例模式
    private static final class InnerExecutorService {
        private static final MyScheduledExecutor INSTANCE = new MyScheduledExecutor();
    }
    public static MyScheduledExecutor getInstance() {
        return InnerExecutorService.INSTANCE;
    }

    public ConcurrentHashMap<String, Future> getFutureMap() {
        return futureMap;
    }

    public void shutdown() {
        executorService.shutdown();
    }

    /**
     * 执行任务
     * @param runnable {@code Runnable}
     */
    public void execute(Runnable runnable) {
        executorService.execute(runnable);
    }

    /**
     * 执行延时任务
     *
     * @param runnable {@code Runnable}
     * @param delay    延迟时间
     * @param timeUnit 时间单位
     */
    public void scheduler(Runnable runnable, long delay, TimeUnit timeUnit) {
        executorService.schedule(runnable, delay, timeUnit);
    }

    /**
     * 执行延时周期性任务scheduleAtFixedRate
     *
     * @param runnable     {@code ScheduledExecutorService.JobRunnable}
     * @param initialDelay 延迟时间
     * @param period       周期时间
     * @param timeUnit     时间单位
     * @param <T>          {@code ScheduledExecutorService.JobRunnable}
     */
    public <T extends JobRunnable> void scheduleAtFixedRate(T runnable, long initialDelay, long period, TimeUnit timeUnit) {
        Future future = executorService.scheduleAtFixedRate(runnable, initialDelay, period, timeUnit);
        futureMap.put(runnable.getJobId(), future);
    }

    /**
     * 执行延时周期性任务scheduleWithFixedDelay
     *
     * @param runnable     {@code ScheduledExecutorService.JobRunnable}
     * @param initialDelay 延迟时间
     * @param period       周期时间
     * @param timeUnit     时间单位
     * @param <T>          {@code ScheduledExecutorService.JobRunnable}
     */
    public <T extends JobRunnable> void scheduleWithFixedDelay(T runnable, long initialDelay, long period, TimeUnit timeUnit) {
        Future future = executorService.scheduleWithFixedDelay(runnable, initialDelay, period, timeUnit);
        futureMap.put(runnable.getJobId(), future);
    }

    public static abstract class JobRunnable implements Runnable {
        private String jobId;

        public JobRunnable(String jobId) {
            this.jobId = jobId;
        }

        public void terminal() {
            try {
                Future future = MyScheduledExecutor.getInstance().getFutureMap().remove(jobId);
                future.cancel(true);
            } finally {
                System.out.println("jobId " + jobId + " had cancel");
            }
        }

        public String getJobId() {
            return jobId;
        }
    }
}

调用示例

public static void main(String[] args) throws Exception {

    MyScheduledExecutor service = MyScheduledExecutor.getInstance();
    service.execute(() -> System.out.println("execute"));
//    service.scheduler(new Runnable() {
//        @Override
//        public void run() {
//            for (Map.Entry<String, Future> next : service.getFutureMap().entrySet()) {
//                String key = next.getKey();
//                int i = Integer.parseInt(key.substring(3));
//                // 停止部分线程
//                if (i % 2 == 0) {
//                    next.getValue().cancel(true);
//                }
//            }
//        }
//    }, 20, TimeUnit.SECONDS);

    for (int i = 0; i < 5; i++) {
        int num = new Random().nextInt(500);
        service.scheduleAtFixedRate(new MyScheduledExecutor.JobRunnable("scheduleAtFixedRate" + num) {
            @Override
            public void run() {
                System.out.println(num);
            }
        }, 10, 2, TimeUnit.SECONDS);
    }
    Thread.sleep(15000);
    for (Map.Entry<String, Future> next : service.getFutureMap().entrySet()) {
        String key = next.getKey();
        int i = Integer.parseInt(key.substring(3));
        // 停止部分线程
        if (i % 2 == 0) {
            next.getValue().cancel(true);
        }
    }
    Thread.sleep(20000);
    service.shutdown();
}

总结

需要注意,通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以如果想保住任务都一直被周期执行,那么catch一切可能的异常。

如果文章的内容对你有帮助,欢迎关注公众号:优享JAVA(ID:YouXiangJAVA),那里有更多的技术干货,并精心准备了一份程序员书单。期待你的到来!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
浅析java常用的设计模式(doc 23页) 1、工厂模式:客户类和工厂类分开。消费者任何候需要某种产品,只需向工厂请求即 可。消费者无须修改就可以接纳新产品。缺点是当产品修改,工厂类也要做相应的修 改。如:如何创建及如何向客户端提供。   2、建造模式:将产品的内部表象和产品的生成过程分割开来,从而使一个建造过程 生成具有不同的内部表象的产品对象。建造模式使得产品内部表象可以独立的变化,客 户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。   3、工厂方法模式:核心工厂类不再负责所有产品的创建,而是将具体创建的工作交 给子类去做,成为一个抽象工厂角色,仅负责给出具体工厂类必须实现的接口,而不接 触哪一个产品类应当被实例化这种细节。   4、原始模型模式:通过给出一个原型对象来指明所要创建的对象的类型,然后用复 制这个原型对象的方法创建出更多同类型的对象。原始模型模式允许动态的增加或减少 产品类,产品类不需要非得有任何事先确定的等级结构,原始模型模式适用于任何的等 级结构。缺点是每一个类都必须配备一个克隆方法。   5、单例模式:单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统 提供这个实例单例模式。单例模式只应在有真正的"单一实例"的需求才可使用。   6、适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从 而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参 数返还一个合适的实例给客户端。   7、桥梁模式:将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们 之间的强关联变成弱关联,也就是指在一个软件系统的抽象化和实现化之间使用组合/聚 合关系而不是继承关系,从而使两者可以独立的变化。   8、合成模式:合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。 合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表 示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同 等看待。   9、装饰模式:装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个 替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态 的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。 一个代理对象,并由代理对象控制对源对象的引用。代理就是一个人或一个机构代表另 一个人或者一个机构采取行动。某些情况下,客户不想或者不能够直接引用一个对象, 代理对象可以在客户和目标对象直接起到中介的作用。客户端分辨不出代理主题对象与 真实主题对象。代理模式可以并不知道真正的被代理对象,而仅仅持有一个被代理对象 的接口,这候代理对象不能够创建被代理对象,被代理对象必须有系统的其他角色代 为创建并传入。   13、责任链模式:在责任链模式中,很多对象由每一个对象对其下家的引用而接   起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。 客户并不知道链上的哪一个对象最终处理这个请求,系统可以在不影响客户端的情况下 动态的重新组织链和分配责任。处理者有两个选择:承担责任或者把责任推给下家。一 个请求可以最终不被任何接收端对象所接受。   14、命令模式:命令模式把一个请求或者操作封装到一个对象中。命令模式把发出 命令的责任和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和 发送的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请 求是怎么被接收,以及操作是否执行,何被执行以及是怎么被执行的。系统支持命令 的撤消。   15、解释器模式:给定一个语言后,解释器模式可以定义出其文法的一种表示,并 同提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。解释器模 式将描述怎样在有了一个简单的文法后,使用模式设计解释这些语句。在解释器模式里 面提到的语言是指任何解释器对象能够解释的任何组合。在解释器模式中需要定义一个 代表文法的命令类的等级结构,也就是一系列的组合规则。每一个命令对象都有一个解 释方法,代表对命令对象的解释。命令对象的等级结构中的对象的任何排列组合都是一 个语言。   16、迭代子模式:迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内 部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容 器对象。迭代子模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭 代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象, 每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。   17、调停者模式:调停者模式包装了一系列对象相互作用的方式,使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值