Question

I have two ExecutorServices, one to hold producers and the other one to hold consumers. I'm using the awaitTermination method, which is blocking and needs a timeout parameter. But I want to wait on both ExecutorServices with the same timeout. As the awaitTermination call is blocking, I can't do:

    this.producersExecutorService.awaitTermination(4, TimeUnit.HOURS);
    this.consumersExecutorService.awaitTermination(4, TimeUnit.HOURS);

Because that would, eventually, wait for a total of 8 hours. What should I do?

Was it helpful?

Solution

If you're waiting for both of them, just await either of them with a timeout of 4 hours, then await the other one with however much time is remaining. How you determine that is up to you - I'd probably work out the desired end time based on the current time before the first call to awaitTermination, and then work out the remaining time based on the difference between that and the current time.

It's not clear from the documentation what happens if you pass in a negative time - you should probably investigate that, and potentially take precautions (e.g. if the first call finishes at exactly the timeout, and then you end up computing the second timeout a millisecond later).

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