public class ThreadPool {
private final static ExecutorService es = Executors.newCachedThreadPool();
public static ExecutorService getExecutor() {
return es;
}
}
public class MyJob {
//间隔秒数
public static void startJob(long times) {
ThreadPool.getExecutor().execute(()->{
long nextTime = new Date().getTime()+times;
while(true) {
long currTime = new Date().getTime();
if(currTime>=nextTime) {
nextTime = currTime + times;
System.out.println("执行时间:"+new Date().toGMTString());
}
}
});
}
public static void main(String[] args) {
MyJob.startJob(1000);
System.out.println("主线程结束--");
}
}