Brief
Java uses the thread class to represent threads, and all threads must be instances of thread or its subclasses.
The task of each thread is to complete a certain task, in effect, to execute a program FLOW.
"to Create and start multi-threaded steps (integrated thread class mode)"
1. Define the subclass that inherits the thread class, overriding the run Method: the Run () method represents the task that the thread needs to complete, so the run () method is called the thread execution Body.
2. Create a thread object: creates an instance of the thread subclass.
3. Start thread: Call the start () method of the thread Object.
"sample Code firstthread.java"
package COM. higgin.part01;//1.1 defines the subclass that inherits the thread class Firstthreadclass firstthread extends Thread{//1.2 Overriding the Run method public void Run() { for(intI=0;i<Ten; i++){ //note: This is used directly when a thread inherits the thread class. GetName () to get the name of the current threadSystem. out. println ( this.GetName()+"===="+i); } }} public classdemo1{ public Static voidmain (string[] Args) {//2. Create a Thread object firstthread t1=New firstthread(); //3. Start the threadt1. Start (); //2. Create a Thread object firstthread T2=New firstthread(); //3. Start the threadt2. Start (); for(intI=0;i<Ten; i++){ //call Thread.currentthrad (). getName () to get the name of the current threadSystem. out. println (thread.currentthread (). getName ()+"===="+i); } }}
"run results"
"note one: The main thread do not forget"
When the Java program starts running, the program will create at least one main thread, and don't forget that the method body of the main () method in this example is the thread execution body of the main threads.
"note Two: two ways to read a Thread's name"
1. Thread.CurrentThread (): CurrentThread () is a static method of the thread class that gets the thread object of the currently executing THREAD.
2.getName (): This method is an instance method of the thread class that returns the name of the thread that called the Method.
"note three: about the thread name"
1. By default, The main thread name is main, and the user-initiated threading name Is: Thread-0, Thread-1 ...
2. of course, You can set the name of the corresponding thread by SetName (String name).
02_ Thread creation and startup _ Inherit the thread way