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.
Loading...
Related Quiz
- Which of the following is an invalid variable name in Java?
- Consider a scenario where a very large number of string concatenation operations are being performed in a single-threaded application. Which class would be appropriate to use for string manipulation, and why?
- How would you handle a situation where a task submitted to ExecutorService is stuck or running for too long?
- What is the worst-case time complexity of Linear Search?
- Consider a scenario where a superclass method that throws an exception is overridden in the subclass. How should the exception handling be approached in this case, ensuring that the application's robustness is maintained?