Waiting for Previously Scheduled Tasks to Terminate

After scheduling tasks to execute, you’ll typically want to wait for the tasks to complete—for example, so that you can use the tasks’ results. After calling method shutdown, you can call ExecutorService method awaitTermination to wait for scheduled tasks to complete. We demonstrate this in Fig. 23.7. We purposely did not call awaitTermination in Fig. 23.4 to demonstrate that a program can continue executing after the main thread terminates.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.