java定时器高级用法1
import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class T implements Runnable {private String jobName = "";public T(String jobName) {super();this.jobName = jobName;}@Overridepublic void run() {System.out.println("execute " + jobName);}public static void main(String[] args) {ScheduledExecutorService service = Executors.newScheduledThreadPool(10);long initialDelay1 = 1;long period1 = 1; // 从现在开始1秒钟之后,每隔1秒钟执行一次job1service.scheduleAtFixedRate( new T("job1"), initialDelay1,period1, TimeUnit.SECONDS);long initialDelay2 = 1;long delay2 = 1; // 从现在开始2秒钟之后,每隔2秒钟执行一次job2service.scheduleWithFixedDelay( new T("job2"), initialDelay2,delay2, TimeUnit.SECONDS);}}