1- multithreaded two implementations

Package Penalty for org.ks. multithreading; 

/ *      
* multithreading (way multithreaded program implementation 1) (master) 
* 1. inherit the Thread 
    * defined class inherits the Thread 
    * override the run method 
    * to write a new thread to do in run method 
    * create thread object 
    * open a new thread, internal method performed automatically run 
* / 


// phenomenon: bb and ksdsb alternately perform 

public  class Demo2_Thread_realize { 

    public  static  void main (String [] args) {
         // method 1 
        / * 
        MyThread mt = new MyThread (); // Create thread class subclass object 
        mt.start (); // 5 open thread. 
        * / 
        
        // method II 
        myRunnable of Mr = new new myRunnable ();     //4. Create Runnable subclass object
         // Runnable object does not start method, so as to Runnable object parameters, the object created by a Thread class Thread class constructor Thread (Runnable target), then call start method 
        Thread T = new new Thread ( of Mr);        // 5. the Runnable the subclass object passed as parameters to the constructor function thread: Runnable target of Mr =; 
        t.start ();                         // 6. the open thread 
        
        for ( int I = 0; I <1000; ++ I)         // 3. write the code to be executed in the run method 
        { 
            System.out.println ( "BB" ); 
        } 
    } 

} 

class the MyThread the extends Thread {            // 1. Thread class inheritance 
    public void run ()                     // 2. A method override run 
        {
             for ( int I = 0; I <1000; I ++)         // 3. write the code to be executed in the run method 
            { 
                System.out.println ( "ksdsb" ) ; 
            } 
        } 
} 


/ *      
* multithreading (way multithreaded program implementation 2) (master) 
* 2. implement Runnable 
    * defining class implements Runnable interface 
    * implement the run method 
    * to write a new thread to do in the run method 
    * Runnable create a custom subclass objects 
    * create a thread object, passing in the Runnable 
    * start () call to open a new thread, inside will automatically call the run Runnable () method 
* / 

class myRunnable the implements{Runnable             // 1. Define a class that implements Runnable 

    @Override                      // 2. override the run method 
    public  void run () {            // 3. will write a new thread to do in the run method 
        
    } 
    
}

 

Guess you like

Origin www.cnblogs.com/kesheng/p/12602274.html