Thread collaboration

Note: It takes practice to master

I. Producer – consumer model –> pipe-process method

Roles: producer, consumer, buffer, product

Two threads share the same object. Each thread can operate within the allowed conditions and wake up when the other thread meets the conditions.

/ / main thread
public class TestMG{
	public static void main(String[] args){
        Basket b = new Basket();
        
        new Girlfriend(b).start();
        newMe(b).start(); }}Copy the code
class Orange{
    int num;
    public Orange(int num){
        this.num = num; }}Copy the code
class Girlfriend extends Thread{
    private Bastek b;
    public Girlfriend(Bastek b){
        this.b =b;
    }
    @Override
    public void run(a){
        for(int i = 0; i<100; i++ ){ System.out.println("Girlfriend peeled off."+i+"An orange.");
            Orange o = newOrange(i); b.push(o); }}}Copy the code
class Me extends Thread{
   Bastek b;
    public Me(Bastek b){
        this.b =b;
    } 
    
    @Override
    public void run(a){
		for(int i =0; i<100 ; i++){
            System.out.println("I ate it."+b.pop().num+"An orange."); }}}Copy the code
class Basket{
    Orange[] oranges  = new Orange[10];
    int count ==0;
 // The producer will peel the orange
    public synchronized void push(Orange orange){
        / / if
    	if(count==oragnes.length){
     	   try{
     	       this.wait();
    	    }catch(InterruptedException e){
			 	e.printStackTrace();
    	     }
        }
 	     oranges[count] = orange;
     	 this.notifyAll();
    	 count++
    }   
 // Consumers want oranges
        public synchronized Orange pop(a){
            if(count ==0) {try{
                    this.wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            count--;
            Orange orange = oranges[count]; 
            this.notifyAll();
            returnorange; }}Copy the code

Two. Signal light method

Roles: producer, consumer, intermediary

Personal understanding: it is a simplified version of the pipe process method. The container has only one position, no position can only be consumed, there is a position can only be produced, set the status bit, reduce the original multiple positions to 1

public class TestGM2{
    public static void main(String[] args){
        Led led = new Led();
    
        new Girlfriend(led).start();
        newMe(led).start(); }}Copy the code
class Girlfriend extends Thread{
    Led led;
    public Girlfriend(Led led){
        this.led = led;
    }
    @Override
    public void run(a){
        for(int i = 0; i<20; i++ ){if(i%2= =0) {this.led.push("Orange");
         }else{
             this.led.push("Banana"); }}}}Copy the code
class Me extends Thread{
 Led led;
    public Me(Led led){
        this.led = led;
    } 
    @Override
    public void run(a){
 		for(int i = 0; i<20; i++ ){this.led.pop(); }}}Copy the code
class Led {
    String o;
    boolean flag = false;
    public synchronized void push(String o){
        if(! flag){try{
                this.wait();
            }catch(InterruptedExcuption e){
                e.printStackTrace();
            }
        }
        System.out.println("Girlfriend peeled off."+s);
        notifyAll();
        this.o =o;
        this.flag = !this.flag;
    
    }
    public synchronized void pop(a){
        if(flag){
            try{
                this.wait();
            }catch(InterruptedException e){
                e.ptintStaticTrace();
            }
        }
        System.out.println("I ate it." + this.o);
        notifyAll();
        this.flag = !this.flag; }}Copy the code