zoukankan      html  css  js  c++  java
  • 任务调度(三)——Timer的替代品ScheduledExecutorService简单介绍

           先前的两篇博文《任务调度(一)——jdk自带的Timer》和《任务调度(二)——jdk自带的Timer 动态改动任务运行计划》中,简介了一下Timer,能够实现几本的功能。可是在多线程方面却略显不足。


           依据Timer源代码,能够看到Timer是单线程的。

    所以task都是串行运行。

    假如当中一个task运行须要非常长的时间,那其它的task仅仅能干巴巴的等着。怎么办!


          如今就遇到了这种问题。

    总不能由于这个小小的问题就去换别的任务调度框架吧,还是想用最简单的方案去解决一下。所以ScheduledExecutorService就被我选中了。这个是怎么找到的?1.网上搜,2.好好的看Timer类的凝视:


           翻译一下:java5.0引入了java.util.concurrent包。当中java.util.concurrent.scheduledthreadpoolexecutor就是在并发有用工具当中之中的一个。scheduledthreadpoolexecutor是一个能够反复运行任务的线程池,而且能够指定任务的间隔和延迟时间。它作为比Timer/TimerTask更加通用的替代品。由于它同意多个服务线程,接受不同的时间单位,且不须要继承TimeTask(只须要实现Runnable接口)。配置ScheduledThreadPoolExecutor为单线程,则与使用Timer等效。


           ScheduledThreadPoolExecutor实现了ScheduledExecutorService接口。所以标题中使用了接口的名字。

           ScheduledExecutorService提供了4个方法:


           当中第二个方法比較特殊一点,第一个參数是Callable。别的都是Runnable,二者的差别不再这篇博文的讨论范围之内。就此略过。说一些其它三个方法。

           schedule()方法第一个參数是任务实例,第二个參数是延迟时间,第三个是时间单元。

    比方调用例如以下:

    	ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
    	pool.schedule(task1, 5, TimeUnit.SECONDS);//延迟5s后,运行且仅仅运行一次task1

           scheduleAtFixedRate()和scheduleWithFixedDelay方法參数是一样的。

    第一个參数是任务实例,第二个參数是延迟时间。第三个是间隔时间,第四个是时间单元。

    这两个方法的不同之处在方法名也能看得出来:scheduleAtFixedRate方法是依照固定频率去运行任务的。而scheduleWithFixedDelay方法则是依照固定的延迟去运行任务。

    /** 
     * task1
     * 
     * @author arron
     * @date 2015年8月5日 下午2:08:34 
     * @version 1.0 
     */
    public class Task1 implements Runnable{
    
    	@SuppressWarnings("deprecation")
    	public void run() {
    		System.out.println("----task1 start--------"+new Date().toLocaleString());
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		System.out.println("----3s later, task1 end--------"+new Date().toLocaleString());
    	}
    	
    }
    測试scheduleAtFixedRate方法:
    	public static void main(String[] args) {
    		
    		ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
    		
    		Task1 t1 = new Task1();
    		//马上运行t1,3s后任务结束,再等待2s(间隔时间-消耗时间),假设有空余线程时,再次运行该任务
    		pool.scheduleAtFixedRate(t1, 0, 5, TimeUnit.SECONDS);
    		
    	}	
           运行结果如图:

           task1第二次运行的前提是,当前有空余的线程。

           运行的開始时间则是上一次结束时间+(间隔时间-任务消耗的时间)。

    增加这个差值小于0。即间隔时间小于任务消耗的时间,那就不会再等待,会马上运行(当然得满足前提)。


    測试scheduleAtFixedRate方法:

    	public static void main(String[] args) {
    		
    		ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
    		
    		Task1 t1 = new Task1();
    		//马上运行t1,3s后任务结束。再等待5s(间隔时间-消耗时间)。假设有空余线程时,再次运行该任务
    		pool.scheduleWithFixedDelay(t1, 0, 5, TimeUnit.SECONDS);
    		
    	}	
           运行结果如图:


           就简介到这里,下篇将会分享替换Timer的代码。


  • 相关阅读:
    构造方法
    封装 private
    局部变量和成员变量区别
    IOC
    Linux端口占用查询命令
    Nginx小白入门实战
    SQL left join right join inner join之间的区别
    IDEA导入maven工程时,不会自动识别怎么办
    Spring中Controller层中的method显示为灰色并且提示method is never used的原因
    查看服务器公网IP
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7142566.html
Copyright © 2011-2022 走看看