转自http://ketqi.blog.51cto.com/1130608/687681
ScheduledExecutorService扩展了ExecutorService接口,提供时间排程的功能。
schedule(Callable<V> callable, long delay, TimeUnit unit) 创建并执行在给定延迟后启用的 ScheduledFuture。 |
schedule(Runnable command, long delay, TimeUnit unit) 创建并执行在给定延迟后启用的一次性操作。 |
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit) 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 |
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit) 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
|
schedule方法被用来延迟指定时间来执行某个指定任务。如果你需要周期性重复执行定时任务可以使用scheduleAtFixedRate或者scheduleWithFixedDelay方法,它们不同的是前者以固定频率执行,后者以相对固定频率执行。
不管任务执行耗时是否大于间隔时间,scheduleAtFixedRate和scheduleWithFixedDelay都不会导致同一个任务并发地被执行。唯一不同的是scheduleWithFixedDelay是当前一个任务结束的时刻,开始结算间隔时间,如0秒开始执行第一次任务,任务耗时5秒,任务间隔时间3秒,那么第二次任务执行的时间是在第8秒开始。
ScheduledExecutorService的实现类,是ScheduledThreadPoolExecutor。ScheduledThreadPoolExecutor对象包含的线程数量是没有可伸缩性的,只会有固定数量的线程。不过你可以通过其构造函数来设定线程的优先级,来降低定时任务线程的系统占用。
特别提示:通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常。
1 package test; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 import java.util.concurrent.ScheduledThreadPoolExecutor; 5 import java.util.concurrent.TimeUnit; 6 7 /** 8 * create at 11-10-14 9 * @author KETQI 10 * @category 11 */ 12 public class TestScheduledThreadPoolExecutor { 13 private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 14 public static void main(String[] args) { 15 //ScheduledExecutorService exec=Executors.newScheduledThreadPool(1); 16 ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); 17 /** 18 *每隔一段时间打印系统时间,互不影响的<br/> 19 * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;<br/> 20 * 也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,<br/> 21 * 接着在 initialDelay + 2 * period 后执行,依此类推。 22 */ 23 exec.scheduleAtFixedRate(new Runnable() { 24 public void run() { 25 System.out.println(format.format(new Date())); 26 } 27 }, 1000, 5000, TimeUnit.MILLISECONDS); 28 29 //开始执行后就触发异常,next周期将不会运行 30 exec.scheduleAtFixedRate(new Runnable() { 31 public void run() { 32 System.out.println("RuntimeException no catch,next time can't run"); 33 throw new RuntimeException(); 34 } 35 }, 1000, 5000, TimeUnit.MILLISECONDS); 36 37 //虽然抛出了运行异常,当被拦截了,next周期继续运行 38 exec.scheduleAtFixedRate(new Runnable() { 39 public void run() { 40 try{ 41 throw new RuntimeException(); 42 }catch (Exception e){ 43 System.out.println("RuntimeException catched,can run next"); 44 } 45 } 46 }, 1000, 5000, TimeUnit.MILLISECONDS); 47 48 /** 49 * 创建并执行一个在给定初始延迟后首次启用的定期操作,<br/> 50 * 随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。 51 */ 52 exec.scheduleWithFixedDelay(new Runnable() { 53 public void run() { 54 System.out.println("scheduleWithFixedDelay:begin,"+format.format(new Date())); 55 try { 56 Thread.sleep(2000); 57 } catch (InterruptedException e) { 58 e.printStackTrace(); 59 } 60 System.out.println("scheduleWithFixedDelay:end,"+format.format(new Date())); 61 } 62 },1000,5000,TimeUnit.MILLISECONDS); 63 64 /** 65 * 创建并执行在给定延迟后启用的一次性操作。 66 */ 67 exec.schedule(new Runnable() { 68 public void run() { 69 System.out.println("The thread can only run once!"); 70 } 71 },5000,TimeUnit.MILLISECONDS); 72 } 73 }
View Code