This is a follow-up to my previous question: How to implement multi-threaded polling
In summary, I'm planning on using an ExecutorService and creating a fixed size pool. Then submitting a batch of tasks and waiting for them to complete. However, it is important that my application supports a shutdown feature that could cancel running tasks - but it must be done cleanly and in a predictable manner
My tasks consist of several steps:
3. notifications <=== point of no return
4. store details in db
... the early steps can be aborted with no ill effects, but after the task reaches a certain stage, I need to ensure the app waits for it to complete. Otherwise, it would leave things in an inconsistent state.
How can I ensure tasks either run to completion or are cancelled before the point of no return ie step 3? I've been reading up on ExecutorService.shutdown/s
() and Future.cancel(..) but the API's seem to filled with a lot of disclaimers like "attempt to..." and "not guaranteed...". Is this even possible?
Any thoughts or advice would be GREATLY appreciated.