JAVA producer consumer of thread synchronization code learning example

  • 2020-04-01 02:32:42
  • OfStack

I. problem description

The producer-consumer problem is a typical thread synchronization problem. The producer produces goods and puts them into the container, the container has a certain capacity (can only be put in order, first put before take), the consumer consumes the goods, when the container is full, the producer waits, when the container is empty, the consumer waits. Notify the consumer when the producer has placed the commodity in the container; When the consumer takes the goods, the producer is notified.

Ii. Solutions

The container resource is locked, and only after the lock is obtained can the operation be performed on the mutually exclusive resource.


public class ProducerConsumerTest {

    public static void main(String []args){
        Container con = new Container();
        Producer p = new Producer(con);
        Consumer c = new Consumer(con);
        new Thread(p).start();
        new Thread(c).start();
    }
}

class Goods{
    int id;
    public Goods(int id){
        this.id=id;
    }

    public String toString(){
        return " goods "+this.id;
    }
}
class Container{//The container adopts stack, first and then out
    private int index = 0;
    Goods[] goods = new Goods[6];

    public synchronized void push(Goods good){
        while(index==goods.length){//When the container is full, the producer waits
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        goods[index]=good;
        index++;
        notifyAll();//The producer notifies the consumer when the commodity is put in
    }

    public synchronized Goods pop(){
        while(index==0){//When there is no commodity in the container it is waiting
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        index--;
        notifyAll();//The producer is notified when the consumer has consumed the commodity
        return goods[index];
    }
}
class Producer implements Runnable{

    Container con = new Container();
    public Producer(Container con){
        this.con=con;
    }

    public void run(){
        for(int i=0; i<20; i++){
            Goods good = new Goods(i);
            con.push(good);
            System.out.println(" Production: "+good);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
class Consumer implements Runnable{

    Container con = new Container();
    public Consumer(Container con){
        this.con=con;
    }

    public void run(){
        for(int i=0; i<20; i++){
            Goods good=con.pop();
            System.out.println(" Consumer: "+good);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    
}


Related articles: