First inherit the thread class, and then override the run () method of the thread class.
The object of the child class of the thread class calls the start () method, and then the virtual machine calls the thread's run () method.
When the program executes to the start () method, the thread starts, there are two execution paths at this time, one is the main method executes the main method, the other is the code in the thread path execution thread run (), and two paths are executed alternately (alternating execution refers to the Snatch Cup execution time, so each execution result is different)
classThreaddemoextendsthread{ Public voidrun ()//store thread to execute the code { for(inti = 0; I < 60; i++) {System.out.println ("Threads" +i); } } }classthreadtest{ Public Static voidMain (string[] args) {Threaddemo thread=NewThreaddemo (); Thread.Start ();//The thread starts and executes the thread's run () method, and the main path and thread are executed alternately for(inti = 0; I < 60; i++) {System.out.println ("Mian" +i); } }}
Thread--Inherit thread