Consider a scenario where multiple threads are executing tasks, but the main thread needs to wait until all tasks are complete before proceeding. How can this be achieved using Future and ExecutorService?

  • Use ExecutorService's awaitTermination() method to block the main thread until all tasks are completed.
  • Use ExecutorService's invokeAny() method to submit tasks and block the main thread until any one of the tasks completes.
  • Use ExecutorService's shutdown() method to ensure that all tasks are complete before proceeding with the main thread.
  • Use invokeAll() method of ExecutorService to submit tasks and obtain a list of Future objects, then call get() method on each Future object to block the main thread until tasks are complete.
In this scenario, you can use the invokeAll() method to submit tasks and obtain a list of Future objects representing each task. Calling the get() method on each Future object will block the main thread until all tasks are complete. This allows synchronization between the main thread and the worker threads.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *