ScheduledExecutorService 一个定长线程池,支持定时及周期性任务执行

ScheduledExecutorService的主要作用就是可以将定时任务线程池功能结合使用。

由于是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。

需要注意,只有当调度任务来的时候,ScheduledExecutorService才会真正启动一个线程,其余时间ScheduledExecutorService都是出于轮询任务的状态。


创建ScheduledExecutorService线程池的两种方式:

第一种 通过Executors 来创建

Executors利用工厂模式向我们提供了4种线程池实现方式,但是并不推荐使用,原因是使用Executors创建线程池不会传入这个参数而使用默认值所以我们常常忽略这一参数,而且默认使用的参数会导致资源浪费,不可取。


 
 
  1. ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  2. ScheduledExecutorService mScheduledExecutorService = Executors.newScheduledThreadPool(num);

num 在的意思是线程池中保持num个线程可以同时执行,但是注意,并不是说线程池中永远都是这三个线程,只是说可以同时存在的线程数,当某个线程执行结束后,会有新的线程进来。

 

第二种方式 也是阿里代码规范推荐使用的   


 
 
  1. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( 1,
  2. 1, 0L,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
  3.  ScheduledExecutorService newScheduledThreadPool =
  4. Executors.newScheduledThreadPool( 4,threadPoolExecutor.getThreadFactory());

 
 
  1. //org.apache.commons.lang3.concurrent.BasicThreadFactory
  2.     ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( 1,
  3.         new BasicThreadFactory.Builder().namingPattern( "example-schedule-pool-%d").daemon( true).build());
  4.  ScheduledExecutorService falseexecutorService  = new ScheduledThreadPoolExecutor(num);

daemon(true)  是守护线程的意思 

在Java中有两类线程:用户线程 (User Thread)、守护线程 (Daemon Thread)。

所谓守护线程,是指在程序运行的时候在后台提供一种通用服务的线程,比如垃圾回收线程就是一个很称职的守护者,并且这种线 程并不属于程序中不可或缺的部分。因此,当所有的非守护线程结束时,程序也就终止了,同时会杀死进程中的所有守护线程。反过来说,只要任何非守护线程还在运行,程序就不会终止。

用户线程和守护线程两者几乎没有区别,唯一的不同之处就在于虚拟机的离开:如果用户线程已经全部退出运行了,只剩下守护线程存在了,虚拟机也就退出了。 因为没有了被守护者,守护线程也就没有工作可做了,也就没有继续运行程序的必要了。

将线程转换为守护线程可以通过调用Thread对象的setDaemon(true)方法来实现。在使用守护线程时需要注意一下几点:

        (1) thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在           运行的常规线程设置为守护线程。 

        (2) 在Daemon线程中产生的新线程也是Daemon的。

        (3) 守护线程应该永远不去访问固有资源,如文件、数据库,因为它会在任何时候甚至在一个操作的中间发生中断。


ScheduledExecutorService  的方法

      falseexecutorService.scheduleWithFixedDelay(command, initialDelay, delay, unit)
      falseexecutorService.scheduleAtFixedRate(command, initialDelay, period, unit)
       falseexecutorService.schedule(command, delay, unit)

  1. 参数 command 方法 执行体   

  2. 参数   initialDelay  首次执行的延时时间 

  3. 参数    delay 定时执行的间隔时间 

  4. 参数    unit  单位     

  • 
       
       
    1. // TimeUnit.NANOSECONDS 千分之一微秒的时间单位
    2.          // TimeUnit.MICROSECONDS 千分之一毫秒的时间单位
    3.          // TimeUnit.MILLISECONDS   表示千分之一秒的时间单位
    4.          // TimeUnit.SECONDS 表示一秒的时间单位
    5.          // TimeUnit.MINUTES 表示六十秒的时间单位
    6.          // TimeUnit.HOURS 表示六十分钟的时间单位
    7.          // TimeUnit.DAYS  表示二十四小时的时间单位

方法 scheduleAtFixedRate 循环任务

scheduleAtFixedRate 循环任务严格按照每5秒发起一次  Thread.sleep(3000); 只是对上一个线程的影响 如果有多个线程不影响下个线程的执行 ------->   以上一个任务的开始时间 + 延迟时间 = 下一个任务的开始时间。


 
 
  1.  executorService.scheduleAtFixedRate( new Runnable() {
  2.                  @Override
  3.                  public void run() {
  4.                     System.out.println( "run {{"+Thread.currentThread().getName()+ "}}> "
  5. + System.currentTimeMillis()/ 1000);
  6.                    try {
  7.                         Thread.sleep( 3000);
  8.                     } catch (InterruptedException e) {
  9.                          // TODO Auto-generated catch block
  10.                         e.printStackTrace();
  11.                     }
  12.                 }        
  13.                
  14.             }, 1, 5,   //TimeUnit.MILLISECONDS 表示千分之一秒的时间单位
  15.                  TimeUnit.SECONDS);

输出结果  :


 
 
  1. run {{pool- 1-thread- 1}}> 1551258899
  2. run {{pool- 1-thread- 1}}> 1551258904
  3. run {{pool- 1-thread- 2}}> 1551258909

 


方法 scheduleWithFixedDelay  延时循环任务

 scheduleWithFixedDelay循环任务严格按照每一秒发起一次,sleep(3000)对于任务的开启是有影响的
   以上一个任务的结束时间 + 延迟时间 = 下一个任务的开始时间。


 
 
  1.   executorService.scheduleWithFixedDelay( new Runnable() {
  2.                  @Override
  3.                  public void run() {
  4.                     System.out.println( "run {{"+Thread.currentThread().getName()+ "}}> "
  5. System.currentTimeMillis()/ 1000);
  6.                    try {
  7.                         Thread.sleep( 3000);
  8.                     } catch (InterruptedException e) {
  9.                          // TODO Auto-generated catch block
  10.                         e.printStackTrace();
  11.                     }
  12.                 }        
  13.                
  14.             }, 0, 5,   //TimeUnit.MILLISECONDS 表示千分之一秒的时间单位
  15.                  TimeUnit.SECONDS);

输出结果   :


 
 
  1. run {{pool- 1-thread- 1}}> 1551259165
  2. run {{pool- 1-thread- 1}}> 1551259173
  3. run {{pool- 1-thread- 2}}> 1551259181
  4. run {{pool- 1-thread- 1}}> 1551259189
  5. run {{pool- 1-thread- 1}}> 1551259197

 


方法  schedule  延时任务

schedule   是延时任务 只执行有一次 


 
 
  1.         executorService.schedule(( new Runnable() {
  2.                  @Override
  3.                  public void run() {
  4.                     System.out.println( "run 延时任务>>>> "+ System.currentTimeMillis());
  5.                 }
  6.             }), 3, TimeUnit.MILLISECONDS);

运行结果 :

run 延时任务>>>> 1551259382734
 
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值