Concurrent collaboration: producer consumer model

Keywords: Java

Producer consumer model

Solution 1: Producer / consumer model

  • Producer: the module responsible for production data (the modules here may be: method, object, thread, process)
  • Consumer: the module responsible for processing data (the modules here may be: method, object, thread, process)
  • Buffer: consumers cannot directly use the data of producers. There is a "buffer" between them. Producers put the produced data into the "buffer", and consumers take the data to be processed from the "buffer".

Solution 2: producer consumer mode
With the help of sign bits, it indicates whether the producer can produce and whether the consumer can consume.

Method

java provides three methods to solve the problem of communication between threads

All methods of the Object class can only be used in synchronization methods or synchronization blocks, otherwise an exception will be thrown.

Tube method

  • Producer: multithreaded, waiting when the buffer is full.
  • Consumer: multithreading, waiting when buffer is empty.
  • Buffer: concurrent container (the example is simple and handwritten).
package ThreadClass;

/**
 * Test tube method
 * @author Xing Yu Wang
 * @date 2020 February 18th 2013
 */
public class Cotest {
	public static void main(String[] args) {
		Container con = new Container();
		new Producer(con).start();
		new Consumer(con).start();
	}
}
//Producer
class Producer extends Thread{
	Container con;
	public Producer(Container con) {
		this.con = con;
	}
	public void run() {
		for(int i = 1;i < 100;i++) {
			System.out.println("Production No." + i + "A cake");
			con.push(new cake(i));
		}
	}
}
//Consumer
class Consumer extends Thread{
	Container con;
	public Consumer(Container con) {
		this.con = con;
	}
	public void run() {
		for(int i = 1 ;i < 100;i++) {
			cake temp = con.pop();
			System.out.println("Consumption first" + temp.id + "A cake");
		}
	}
}
//Buffer
class Container{
	cake[] cc = new cake[10];
	int count = 0;
	//production
	public synchronized void push(cake cc) {
		//If the container is full, the thread is blocked and waiting for the consumer to wake up
		if(count == 10) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.cc[count++] = cc;
		//Producers produce and inform consumers of consumption
		this.notifyAll();//Because only one thread is blocking, notify is the same as notifyAll
	}
	//consumption
	public synchronized cake pop() {
		//If the container is empty, the thread blocks and waits for the producer to wake up
		if(count == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		cake temp = this.cc[--count];
		this.notifyAll();
		return temp;
	}
}
//data
class cake{
	int id;
	public cake(int id) {this.id = id;}
}

Signal lamp method

With the help of flags.

package ThreadClass;
/**
 * Test signal lamp method
 * @author Xing Yu Wang
 * @date 2020 February 18th 2013
 */
public class Contest {
	public static void main(String[] args) {
		TV tv = new TV();
		new player(tv).start();
		new watcher(tv).start();
	}
}
//Producer actor
class player extends Thread{
	TV tv;
	public player(TV tv) {
		this.tv = tv;
	}
	public void run() {
		for(int i = 0;i < 10;i++) {
			tv.play("Love is one word, I just say" + (i + 1) + "second");
		}
	}
}
//Consumer audience
class watcher extends Thread{
	TV tv;
	public watcher(TV tv) {
		this.tv = tv;
	}
	public void run() {
		for(int i = 0;i < 10;i++) {
			tv.watch();
		}
	}
}
//Same resource tv
class TV{
	String voice;
	//Signal lamp
	//T means actor performing, audience waiting
	//F means audience watching, actors waiting
	boolean flag = true;
	//perform
	public synchronized void play(String voice) {
		//If the light goes out, wait
		if(!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Performed:" + voice);
		this.voice = voice;
		//Inform the audience to watch
		this.notifyAll();
		//Change signal light
		this.flag = false;
	}
	//watch
	public synchronized void watch() {
		//If the light is on, wait
		if(flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("Heard:" + voice);
		//Inform actors to perform
		this.notifyAll();
		//Change signal light
		this.flag = true;
	}
}
Published 19 original articles, won praise 9, visited 981
Private letter follow

Posted by CorfuVBProgrammer on Mon, 17 Feb 2020 22:26:48 -0800