Question

Recently I got a interview question that:

There is a scenario where one thread executing batch operation in interval of one hour will you use executer framework or normal thread and why ?

I am confused .

As there is only one thread, there is no need of executors service. I can do it using while and sleep.

while(1)
{
// do task
// t1.sleep(60*60*1000);
}

While there is a ScheduleExecutorService which provides so many methods for scheduulling?

What is the best way?

Was it helpful?

Solution

The problem with your solution above is that if your task takes 59mins, then your thread will spend 59mins executing your task, then sleep for an hour. So you'll invoke your task once every (nearly) 2 hours.

If you use the scheduled executor framework, then that'll look after invoking your task every hour on the hour (regardless of how long it takes). Note also that it can handle the scenario in which your task executes for longer than one hour (either by design, or by accident). You can optionally opt to launch a second task in parallel, or skip the subsequent invocation.

I would generally make use of the executor framework. It provides a lot of useful functionality, and you can encapsulate your tasks such that they don't just have to be run in a scheduled executor, but any executor.

OTHER TIPS

Plain thread vs. Executor Service is a false dichotomy for this case. The real question should be, use the old Timer vs. the new ScheduledExecutorService. Even on Java 1.2 it would be entirely superfluous to try to reimplement task scheduling using a plain Thread.

And the answer would again be "use the Executor Service". It's much more convenient, much more flexible and scalable. As the application grows it will be almost trivial to adapt it to ever harder requirements.

Timer == Oldtimer

Really, in today's Java the whole issue of Thread vs. Timer vs. ExecutorService should be stricken out as deprecated. Use ExecutorService and you can't go wrong; use any of the other options and you are painting yourself into a corner.

i think that's depends on what you need.

ScheduleExecutorService has got some useful functions such as

scheduleAtFixedRate: no matter how long your task will run, this call will make sure your task run at fix rate. for example, heartbeating

scheduleWithFixedDelay: this one will add an delay after your task done, for example, some clean job.

ScheduledExecutorService should be a good choice as you need not write the code to schedule the taks periodically. Using a well tested framework is better than writing your own framework. You can rely on ScheduledExecutorService for the periodic execution of task as configured. It will save you time and effort to write the similar functionality.

Other benefits of using ScheduledExecutorService are that you can cancel the task, can get the status of task once it is finished, etc.

ScheduleExecutorService provides you a way to chek/cancel execution by returning ScheduledFuture. If you use a single thread, you will have to implement this yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top