Java定时器小例子

时间:2022-08-25 17:42:55
  
 
 
  1. package com.jelly.timer; 
  2.  
  3. import java.util.concurrent.ScheduledExecutorService; 
  4. import java.util.concurrent.ScheduledThreadPoolExecutor; 
  5. import java.util.concurrent.TimeUnit; 
  6.  
  7. public class ScheduledExecutorServiceExample { 
  8.     public static void main(String[] args) { 
  9.         ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(2); 
  10.         executor.scheduleWithFixedDelay(new Task("51CTO博客.."), 05, TimeUnit.SECONDS); 
  11.         executor.scheduleWithFixedDelay(new Task("你值得一览.."), 205, TimeUnit.SECONDS);// 延迟20秒后再执行 
  12.     } 
  13.  
  14.     static class Task implements Runnable { 
  15.         /** 
  16.          * run 
  17.          */ 
  18.         public void run() { 
  19.             System.out.println(message); 
  20.         } 
  21.  
  22.         public Task(String message) { 
  23.             this.message = message; 
  24.         } 
  25.  
  26.         public final String message; 
  27.     } 

 

本文出自 “简单的jelly” 博客,请务必保留此出处http://jelly8090.blog.51cto.com/6153039/1036030