示例import java.util.*;public class timerthreadtest { public static void main(string []args) { task t1 = new task("task 1"); task t2 = new task("task 2"); timer t = new timer(); t.schedule(t1, 10000); // executes for every 10 seconds t.schedule(t2, 1000, 2000); // executes for every 2 seconds }}class task extends timertask { private string name; public task(string name) { this.name = name; } public void run() { system.out.println("[" + new date() + "] " + name + ": task executed!"); }}
输出[thu aug 01 21:32:44 ist 2019] task 2: task executed![thu aug 01 21:32:46 ist 2019] task 2: task executed![thu aug 01 21:32:48 ist 2019] task 2: task executed![thu aug 01 21:32:50 ist 2019] task 2: task executed![thu aug 01 21:32:52 ist 2019] task 2: task executed![thu aug 01 21:32:53 ist 2019] task 1: task executed![thu aug 01 21:32:54 ist 2019] task 2: task executed![thu aug 01 21:32:56 ist 2019] task 2: task executed![thu aug 01 21:32:58 ist 2019] task 2: task executed![thu aug 01 21:33:00 ist 2019] task 2: task executed!
以上就是我们如何在java中实现一个计时器线程?的详细内容。
