java任务调度Timer简单例子
1、创建类TimerTest
import java.util.Timer; import java.util.TimerTask; public class TimerTest { private final Timer timer = new Timer(); public void start() { timer.schedule(new TimerTask() { public void run() { System.out.println("定时任务"); } }, 0, 2 * 1000); } }
2、运行测试类test
public class Test { public static void main(String[] args){ // TODO Auto-generated method stub TimerTest aa = new TimerTest(); aa.start(); } }