Managing Threads with ExecutorService
To find the directory size, we could split the operation into parts, with each task exploring a subdirectory. Since each task has to run in a separate thread, we could start an instance of Thread. But threads are not reusable—we can’t restart them, and scheduling multiple tasks on them is not easy. We certainly don’t want to create as many threads as the number of subdirectories we find—it’s not scalable and is the quickest way to fail. The java.util.concurrent API was introduced exactly for this purpose—to manage a pool of threads.
We met the ExecutorService and the different types of executors in Concurrent Computation of Net Asset Value. The ExecutorService, the Executors factory, and the related API ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access