The first way to create a thread: Inherit the Thread class
Second way to create a thread: implementing the Runnable Interface
1. Define the class implementation runnable interface.
2. Overwrite the Run method in the interface to encapsulate the thread's task code in the Run method.
3. Create a thread object from the thread class and pass the subclass object of the Runnable interface as a parameter to the constructor in the thread class.
Why? Because the thread's tasks are encapsulated in the Run method of the Runnable interface subclass object. Therefore, when you create a thread object, you must explicitly define the task that you want to run.
4. Call the Thread object's Start method to open the thread.
Benefits of implementing the Runnable interface:
1. Separate the thread's tasks from the child classes of the thread, which are individually encapsulated. Encapsulates a task into an object based on object-oriented thinking.
2. Avoids the limitations of Java single inheritance.
Java Nineth Day