// ===== Task.java ===== import java.util.TimerTask; public class Task extends TimerTask ... { private String id; public Task(String id)...{ this.id = id; } @Override public void run() ...{ System.out.println(id+" is running..."); }} // ===== Test .java ===== import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class Test ... { /** *//** * @param args */ public static void main(String[] args) ...{ Timer timer = new Timer(); TimerTask t1 = new Task("t1"); TimerTask t2 = new Task("t2"); //1000毫秒后,每隔1000毫秒运行一次t1任务 timer.schedule(t1,1000,1000); //从现在开始,每隔500毫秒运行一次t2任务 timer.schedule(t2, new Date(),500); try ...{ Thread.sleep(10000); } catch (InterruptedException e) ...{ // TODO Auto-generated catch block e.printStackTrace(); } timer.cancel(); System.out.println("---end---"); }}