0

I am planning on using ExecutorService for creating a pool of 10 threads and run them in parallel. All these threads accept the same task which return a boolean value. Only one of them will at most return "true". However I want to stop all other threads once one of the threads return true. Is there any threading option that supports this use case by default or should I handle the cancellation of other threads manually?

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • If you're not looking to stop all threads running under an executor service, then post a comment and this question can be repoened. – Tim Biegeleisen Oct 30 '17 at 01:58
  • @TimBiegeleisen , In my case , I want all the 10 threads to run in parellel. If for instance say 2 of these threads finish but if they return value "false" , I would like the other threads to continue running. However if any threads returns value "true" , I would like other threads to stop. I wanted to check if there are any inbuilt capabilities provided by java for handling this scenario. – Punter Vicky Oct 30 '17 at 02:00
  • Use a callback in the threads you run on the service (q.v. [here](https://stackoverflow.com/questions/13116192/java-set-a-callback-from-executorservice)). Then, if one should return true, just follow the duplicate link to shut down the entire service. – Tim Biegeleisen Oct 30 '17 at 02:06
  • Thanks , this example seems to be the one I am looking for - http://blog.teamlazerbeez.com/2009/04/29/java-completionservice/ – Punter Vicky Oct 30 '17 at 03:24

0 Answers0