Design and copy a task scheduler that can run unsynchronized or synchronized tasks
This is an interview question, which means it can be done in a short time. I thought to ask here because I can't figure out what to do if asked.
"Create and configure a task scheduler that can run unsynchronized or synchronized tasks."
Please use your imagination / guess and share your thoughts and comments.
source to share
This question is intentionally vague, it should show how good you are at designing and solving problems, what assumptions you make, how you justify them, etc. There is no single, good answer. It's a matter of getting closer to the problem.
Here's what's said here, my take:
-
My planner can take arbitrary
Runnable
orCallable<V>
, I will useScheduledExecutorService
because it seems like a good abstraction for the problem. I use as many standard classes as possible to make the API portable and easy to use. -
I don't understand how non-violence and synchronization are: it is safe to run at the same time and those that require an exclusive lock. That is, the scheduler is not allowed to run two synchronized tasks at the same time.
-
The distinction between synchronized and non-synchronized tasks will be done using the token interface. Annotation is also fine, but it is more difficult to extract it at runtime.
-
I won't give you the full implementation, but it will probably carry over some standard
ScheduledExecutorService
with extra sync for synchronized tasks. I thinkConcurrentMap<Class, Semaphore>
. Before running tasks marked as synchronized, I make sure that no other synchronized task at the same time is running. I block and wait or reject (this can be customized).
source to share