An introduction to executors
The basic mechanism to implement a concurrent application in Java is:
- A class that implements the Runnable interface: This is the code you want to implement in a concurrent way
- An instance of the Thread class: This is the thread that is going to execute the code in a concurrent way
With this approach, you're responsible for creating and manning the Thread
objects and implementing the mechanisms of synchronization between the threads. However, it can have some problems, especially with those applications with a lot of concurrent tasks. If you create too many threads, you can degrade the performance of your application or even hang the entire system.
Java 5 included the executor framework, to solve these problems and provide an efficient solution, which would be easier for the programmers to use than the traditional concurrency mechanisms.
In this chapter, we will introduce the basic characteristics of the executor framework by implementing the following two...