java随笔一(关于定时任务)

public class ThreadTest {

    class MyTask implements Runnable{

        public void run() {
            say();
        }
    }

    public void init(){
        say();
        MyTask myTask = new MyTask();
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.scheduleAtFixedRate(myTask,5,5,TimeUnit.SECONDS);
    }

    public static void say(){
        System.out.println("Hello world");
    }

    public static void main(String[] args)  {
        ThreadTest threadTest = new ThreadTest();
        threadTest.init();
    }
}

  这个程序执行了之后,在运行这个程序后的5秒(scheduleAtFixedRate的第二个参数决定)之后,每5秒(scheduleAtFixedRate的第三个参数决定)执行一个say().

posted @ 2017-09-21 21:34  蜗牛不爱海绵宝宝  阅读(212)  评论(0编辑  收藏  举报