定时器实现多线程

指定一个线程任务,指定何时执行,指定每隔多久执行一次。其中,任务TimerTask是实现了Runnabe接口的。

package com.roocon.thread.t2;

import java.util.Timer;
import java.util.TimerTask;

public class Demo5 {

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer is running");
            }
        }, 0, 1000);
    }
}

运行结果:

timer is running
timer is running
timer is running
timer is running

 

posted @ 2017-12-07 21:23  凌晨六点半  阅读(1575)  评论(0编辑  收藏  举报