Chapter 30 Multithreading and Parallel Programming
- You can create additional threads to run concurrent tasks in the program.
- In Java, each task is an instance of the Runnable interface, also called a runnable object.
- A thread is essentially an object that facilitates the execution of a task.
Since the Thread class implements Runnable, you could define a class that extends Thread and implements the run method, and then create an object from the class and invoke its start method in a client program to start the thread:
Instead of using the stop() method, you should assign null to a Thread variable to indicate that has stopped.
If a sleep method is invoked in a loop, you should wrap the loop in a try-catch block, as shown in (a) below. If the loop is outside the try-catch block, as shown in (b), the thread may continue to execute even though it is being interrupted.