Multi-threaded control

1, two threads

package com.zy.demo09;
//单例模式
public class A {
    private A() {
        
    }
    private static A a=new A();
    public static A getA(){
        return a;
    }

}
package com.zy.demo09;
public class People {
    static String name;
    static String sex;

}
package com.zy.demo09;
public class MyRunnable01 implements Runnable{
    @Override
    public void run() {
        int a=0;
        while (true) {
            synchronized (A.getA()) {                
                a++;
                if (a%2==0) {
                    People.name="小明";
                    People.sex="男";
                } else {
                    People.name= "Red" ; 
                    People.sex = "female" ; 
                    
                }             
                // wait wakeup To clear who is locked object (the first wake-up wait) 
                A.getA () the Notify ();. // wake each other 
                the try { 
                    A.getA () .wait (); // make yourself wait ---- lost the executive power of the cpu 
                } the catch (InterruptedException E) {
                     // TODO Auto-Generated the catch Block 
                    e.printStackTrace (); 
                } 
                
            } 
    
        } 
    } 
}
Package com.zy.demo09;
 public  class MyRunnbale02 the implements the Runnable { 
    @Override 
    public  void RUN () {
         the while ( to true ) {
             the synchronized (A.getA ()) { 
                System.out.println (People.name); 
                the System.out. println (People.sex);                 
                // wait wake lock object must be clear who is 
                . A.getA () the Notify (); // wake each other 
                the try { 
                    . A.getA () the wait (); // make yourself wait - - lost the executive power of the cpu 
                } the catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }            
            }        
        }        
    }
}
Package com.zy.demo09;
 public  class the Test {
     public  static  void main (String [] args) {    
         // thread wayward 
        the Thread Thread1 = new new the Thread ( new new MyRunnable01 ()); 
        the Thread Thread2 = new new the Thread ( new new MyRunnbale02 ()); 
        thread1.start (); 
        thread2.start (); 
        // wait wake ----- control thread    
         // wait wake of the basic conditions in the thread synchronization environment ------
                 // lock object is to clearly belongs who the synchronized (A.getA ()) {} 
            / *     
                who // wait wake lock object is to clear
                A.getA () the Notify ();. // wake each other 
                the try { 
                A.getA () the wait ();. // make yourself wait ---- lost the executive power of the cpu 
                } the catch (InterruptedException E) { 
                    / / Auto-Generated the TODO the catch Block 
                    e.printStackTrace (); 
                } * / 
    } 
}

The result: a virtual eternal light, have been output format output by red female and male Xiaoming, gender disorder will not happen

2, three threads (one by one)

Sequentially output red, yellow, blue

Package com.zy.exercise;
 public  class A {
     private A () {     
    } 
    static  int In Flag = 1; // In Flag 1 executes the first thread, the second thread execution is 2, 3 for the execution of the third thread 
    private  static A = A new new A ();
     public  static A getA () {
         return A; 
    } 

}
package com.zy.exercise;
public class MyRunnable01 implements Runnable{
    @Override
    public void run() {
        while(true){
            synchronized (A.getA()) {            
                   while(A.flag == 1) {
                       System.out.println("红");
                       A.flag = 2;
                   }
                   A.getA().notify();
                   try {
                       A.getA().wait();
                   }catch(InterruptedException e) {
                       e.printStackTrace();
                   }
            }
        }
        
    }
    

}
package com.zy.exercise;

public class MyRunnable02 implements Runnable{

    @Override
    public void run() {
        while(true){
            synchronized (A.getA()) {
                while(A.flag == 2) {
                    System.out.println("黄");
                    A.flag = 3;
                }
                A.getA().notify();
                try {
                    A.getA().wait();
                }catch(InterruptedException e) {
                    e.printStackTrace (); 
                } 
                
                 } 
            } 
        
    } 

}
package com.zy.exercise;

public class MyRunnable03 implements Runnable{

    @Override
    public void run() {
        while(true){
            synchronized (A.getA()) {
                  
                while(A.flag == 3) {
                    System.out.println("蓝");
                    A.flag=1;
                }
                A.getA().notify();
                try {
                    A.getA().wait();
                }catch(InterruptedException e) {
                    e.printStackTrace (); 
                } 
       
            } 

            } 
        
    } 

}
package com.zy.exercise;

public class Test {

    public static void main(String[] args) throws Exception {
        Thread thread1 = new Thread(new MyRunnable01());
        Thread thread2 = new Thread(new MyRunnable02());
        Thread thread3 = new Thread(new MyRunnable03());
        thread1.start();
        thread2.start();
        thread3.start();
 
   }
}

operation result

Virtual eternal lamp has been in the order of red, yellow, blue output

Guess you like

Origin www.cnblogs.com/qfdy123/p/11122839.html