Created in two ways in multithreaded java

Dian inheritance Thread class implements a multi-threaded

Step One: Thread class inherits 
Step two: Rewrite run () method
Step 3: Create the object inherits the Thread class, call the start () method to start.
// thread way to create a: 
/ * 
Step One: Thread class inherits 
Step two: Rewrite run () method 
Step 3: Create the object inherits the Thread class, call the start () method to start. 
 * / 
Public  class TestThread the extends the Thread { 
    @Override 
    public  void RUN () {
         for ( int I = 0; I <3000; I ++ ) { 
            System.out.println ( "love you three thousand times" ); 
        } 
    } 

    public  static  void main (String [] args) { 
        TestThread TestThread = new new TestThread (); 
         testThread.start (); 
    } 

}

Two Dian achieve Runnable interface to create multiple threads

The first step: implement runnable interfaces 
Step two: red write run method
Step 3: Create implementation class objects, create a proxy class thread class object + start () method to start a thread
public  class Thread_Study the implements Runnable {
 / * 
* The first step: implement runnable interfaces 
* Step two: red write run method 
* Step 3: Create implementation class objects, create a proxy class thread class object + start () method to start a thread 
* 
* * / 

    @Override 
    public  void RUN () {
         for ( int I = 0; I <100; I ++ ) { 
            System.out.println ( "read" ); 
        } 
    } 

    public  static  void main (String [] args) { 
        Thread_Study TS = new new Thread_Study ();
         new new the Thread (TS) .start ();
        for (int i = 0; i < 100; i++) {
            System.out.println("敲代码");
        }

    }


}

operation result:

Code knock
knock Code
knock Code
knock Code
knock Code
knock Code
knock Code
knock Code
reading
reading
reading
reading
reading
reading
reading

in conclusion:

We can observe the code knock and reading at the same time between each thread is not to interfere with each other, each perform their own, and this call different methods, call the method is to wait until the last steps before they can complete in the next step.

 
 

Guess you like

Origin www.cnblogs.com/xiaoqiqistudy/p/10984248.html