public class TestThread1 {public static void main (string[] args) {Runner1 r = new Runner1 (); <span style= "WHITE-SPACE:PR E "></span>//r.run ();//This place does not belong to the Enable new thread, which belongs to the method call thread t = new Thread (r); T.start ();//When a new thread is enabled, You must use the Start method under thread for (int i=0;i<100;i++) {System.out.println ("Main:" + i);}}} Class Runner1 implements Runnable {public void run () {for (int i=0;i<100;i++) {System.out.println ("Runner1:" + i);}}} /* In the JDK document, the method Start () of the class thread is described as follows: public void start () causes the thread to execute; The Java virtual machine calls the thread's Run method. The result is two threads running concurrently, when the front thread (from the call is returned to the Start method) and another thread (executing its Run method). It is illegal to start a thread multiple times. This is especially the time when the thread has finished executing and cannot be restarted. */
Another way to enable a new thread is to inherit the thread class:
public class TestThread1 {public static void main (string[] args) {Runner1 r = new Runner1 (); R.start (); <span style= "whit E-space:pre "></span>//is inherited from the thread class, so you can also use the inherited Start () method//<span style=" White-space:pre ">< /span>r.run ();//This place does not belong to the Enable new thread, which belongs to the method call//thread t = new Thread (r),//t.start ();//When a new thread is enabled, it must be used with the Start method under thread for ( int i=0;i<100;i++) {System.out.println ("Main:" + i);}}} Class Runner1 extends Thread{//class Runner1 implements Runnable {public void run () {for (int i=0;i<100;i++) {System.out . println ("Runner1:" + i);}}}
Inheritance is not recommended, because inheritance can only be single-inheritance, but the interface can implement multiple inheritance, so in Java, the interface can be implemented as far as possible, do not inherit
To enable a new thread