Day13 multi-thread creation method, day13 multi-thread Creation
#01 create multiple threads
Inherit Thread class
Overwrite the run method: run the code to be executed in the run method.
Create object
Call the start method. The start method starts the thread and then calls the run method.
Get the Thread name: getName () method, in the Thread class
The Thread class obtains the reference of the object in the executing Thread. The static Thread currentThread () method returns a Thread object. This object can obtain the name by calling the method to obtain the Thread name.
Thread. currentThread (). getName () Get the Thread name
#02
The second way to create a thread to implement the runnable interface
1. Define a class to implement the runnable interface
2. overwrite the run method of the runnable interface.
3. Create an object that inherits the runnable interface, and then use this object as a parameter to create a Thread object.
4. Call the start method of the thread object to enable the thread.
The second method is generally used.