1)Timer:
例:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("1111");
}
},0,1000);
2)ScheduledExecutorService:
例:
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("11111");
}
}, 0, 2000, TimeUnit.MILLISECONDS);
3)Spring自带的@Schedule或者类似于轻量级quarz的spring-task
4)quarz
其中
Timer是单线程
ScheduledExecutorService是线程池