Timer的使用和参数解释

来源:互联网 发布:架构 指令集 知乎 编辑:程序博客网 时间:2024/06/08 05:12

Timer是一个管理任务队列执行定时任务的类

执行的任务必须是继承TimerTask的线程类

timer.schedule(task, delay, period);

其中 task就是一个TimerTask任务

delay代表延迟执行下一个任务的时间

period代表下一次执行该任务的时间


Timer可以作为线程池使用,也可管理多线程。


public class TestTimer {

public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new TestThread("test1"), 1000, 2000);
timer.schedule(new TestThread("test2"), 2000, 4000);
timer.schedule(new TestThread("test3"), 3000, 5000);
}
}


class TestThread extends TimerTask{
@SuppressWarnings("unused")
private String content = "";
public TestThread(String content){
this.content = content;
}
public void run(){
System.out.println(this.content);
}
}
0 0
原创粉丝点击