October 2019
Intermediate to advanced
434 pages
11h 54m
English
Creating a new Thread for every task is not an ideal approach for managing asynchronous work. The number of threads and associated resources can quickly get out of hand. One way to improve upon this is to make use of ExecutorService. With ExecutorService, we can submit tasks to be run asynchronously and let ExecutorService make decisions about how to manage which threads the tasks are run on.
We can define a specific number of threads to create and reuse for ExecutorService. This limits the amount of resources used, and makes it easier to understand the potential performance impacts of our code.
Creating ExecutorService with a fixed Thread count can be done quite simply, as shown in the following snippet:
val executor =
Read now
Unlock full access