Step: 1. Define a class to inherit the thread. 2. Rewrite the Run method. 3. Create the subclass object, which is the thread object. 4. Call the Start method. The thread is opened and the thread executes, and the JVM is told to call the Run method. (Start does two things, open the thread and call the Run method.) )
- The thread object calls the Run method and calls the Start method difference (interview frequently):
- Calling the Run method does not open the thread. The method is called only by the object.
- Call start to open the thread and have the JVM call the Run method to execute in the open thread.
Instance:
classDemoextendsthread{PrivateString name; Demo (String name) { This. Name =name;} Public voidrun () {Show ();} Public voidShow () { for(inti = 0; I < 20; i++) {System.out.println (name+ "..." +i); }}} Public classThreaddemo { Public Static voidMain (string[] args) {Demo D1=NewDemo ("Xiaoqing"); Demo D2=NewDemo ("Wangcai"); D2.start (); D1.run (); //Thread is numbered starting at 0, where two thread objects are created, but the first thread is not turned on. }}
Create multithreaded mode One (inherit the thread class)