day25 Java multi-threading learning

 Multi-threaded (single design patterns: * master)

    Singleton design pattern: the guaranteed only one object in memory.

     The difference between lazy and hungry Chinese-style style:

         * 1. spatial starving for time; lazy man is time for space. (Not recommended lazy type of use)

         * 2. In multi-threaded access, a hungry man does not create more than one object type, and lazy man is likely to create multiple objects.

      Three separate written:

/ * 
 * Starving formula 
 * / 
class the Singleton {
     // 1. private constructor, other classes can access the class constructor 
    Private the Singleton () {};
     // 2. Create a class object of the present 
    Private   static the Singleton = S new new the Singleton ( );   // member variable is private Singleton s, the class name can not be invoked.
     @ 3 provide external access to a common method 
    public  static the Singleton the getInstance () {        // Get instance 
        return S;         
    }         
} 

/ * 
 * lazy formula: single Example lazy loading mode (single mode not in use, since it is possible to create a plurality of objects) 
 * / 
class the Singleton {
     // 1. private constructor, other classes can access the class constructor 
    privateSingleton () {};
     // 2. declare a reference 
    Private  static singleton S;
     // 3. public access methods provide external 
    public  static singleton the getInstance () {        // Get Example 
      IF (S == null ) { 
         S = new new singleton (); 
           } 
        return S;         
    }         
} 
/ * 
 * third 
 * / 
class singleton {
     // 1. private constructor, other classes can access the class constructor 
    private singleton () {};
     // 2. Create the present class Object 
    public  static  FinalS = singleton new new singleton ();   // can not be changed after the addition Final s 
    
}
Single design patterns

 

  Multithreading (Runtime class)

     Runtime class is a singleton.

    public  static  void main (String [] args) throws IOException {
         // operating the same object the shutdown 
   the Runtime = R & lt Runtime.getRuntime ();    // get the object runtime 
   r.exec ( "the shutdown -s -t 300");       / / modified 
   r.exec ( "the shutdown -a");              // to modify the object according 
    }
example

 

  Multithreading (Timer categories: * Master)

      Timer class: the timer.

public  class Dome3_Timer { 

    public  static  void main (String [] args) throws InterruptedException { 
       
        the Timer T = new new the Timer ();      // Create a timer
         // Schedule (): Specifies the task at the specified time schedule.
        // The first parameter is the specified task, the second parameter is the time of execution, the third parameter is too long repeated execution 
        t.schedule ( new new to MyTimer (), new new a Date (119,. 5,. 6, 0 , 57 is, 0. 4), 2000 );
            the while ( to true ) { 
               the Thread.sleep ( 1000 ); 
            System.out.println ( new new a Date ()); 
        }
    }

}

class MyTimer extends TimerTask{

    @Override
    public void run() {
        
        System.out.println("起来读书");    
    }    
}
schedule()

 

Multithreading (communication between two threads: * Master)

1. When do I need communication:

       * A plurality of threads concurrently executed, the CPU random thread switching by default.

       * If we want them to have to perform regularly, you can use communication.

2. how communication:

       * Wait (): the current thread wait

       * Notify (): wake up the thread of a single random waiting

       * These two methods must be performed in the synchronization code, and the synchronization lock used to invoke the object.

public class Dome1_Notify {

    public static void main(String[] args) {
    final  Printer p=new Printer();
    new Thread() {
          public void run () {
              while(true) {
              try {
                p.print1();
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
          }      
          }
      }.start();
       
      
      newThe Thread () {
           public  void RUN () {
               the while ( to true ) {
               the try { 
                p.print2 (); 
            } the catch (InterruptedException E) {
                 // the catch block TODO automatically generated 
                e.printStackTrace (); 
            } 
          }       
          } 
      } .start (); 
    } 

} 
// wait wakeup mechanism 
class Printer {
     Private   int In Flag =. 1 ;
     public  void PRINT1 () throws{InterruptedException
         the synchronized ( the this ) {
             IF (In Flag =. 1! ) {
                 The this .wait (); 
            } 
                of System.out.print ( "A" ); 
                of System.out.print ( "B" ); 
                of System.out.print ( "C" ); 
                of System.out.print ( "\ R & lt \ n-" ); 
                in Flag = 2 ;
                 the this .notify ();     // random wake up a single thread waiting 
            } 
        } 

    
    public  void print2 ()throws InterruptedException {
         the synchronized ( the this ) {
             IF (In Flag = 2! ) {
                 the this .wait ();      // this.wait (): current thread to wait 
            } 
            of System.out.print ( "D" ); 
            of System.out.print ( "E" ); 
            of System.out.print ( "F" ); 
            of System.out.print ( "\ R & lt \ n-" ); 
            In Flag =. 1 ;
             the this .notify ();           // this.notify (): random wake up a single thread waiting 
             }            
} 
}
example

 

  

 

Guess you like

Origin www.cnblogs.com/feng0001/p/10982871.html
Recommended