What are the differences between, or respective limitations of, java's Timer facility
and ScheduledThreadPoolExecutor service
?
I know that Timer
runs in single
thread, but are there any other limitations apart from that?
What are the differences between, or respective limitations of, java's Timer facility
and ScheduledThreadPoolExecutor service
?
I know that Timer
runs in single
thread, but are there any other limitations apart from that?
Being single threaded is the main limitation of Timer:
timer is not fair with tasks, for example:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Task#1: " + System.currentTimeMillis());
}
}, 1000, 1);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Task#2: " + System.currentTimeMillis());
}
}, 1200, 1);
would demonstrate that task#2 doesn't even get a shot at CPU time.
ScheduledExecutorService
gives you the right level of control over level of concurrency of your tasks.