Java定时器小例子

    package com.jelly.timer; 
     
    import java.util.concurrent.ScheduledExecutorService; 
    import java.util.concurrent.ScheduledThreadPoolExecutor; 
    import java.util.concurrent.TimeUnit; 
     
    public class ScheduledExecutorServiceExample { 
        public static void main(String[] args) { 
            ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(2); 
            executor.scheduleWithFixedDelay(new Task("CSDN博客.."), 0, 5, TimeUnit.SECONDS); 
            executor.scheduleWithFixedDelay(new Task("你值得一览.."), 20, 5, TimeUnit.SECONDS);// 延迟20秒后再执行 
        } 
     
        static class Task implements Runnable { 
            /** 
             * run 
             */ 
            public void run() { 
                System.out.println(message); 
            } 
     
            public Task(String message) { 
                this.message = message; 
            } 
     
            public final String message; 
        } 
    } 


你可能感兴趣的:(JavaBase)