java多線程生產者和消費者

生產者和消費者之一對一

生產者和消費者之一對多

一對一

Clerk 庫存 最大隻能保存20件

Productor 生產者

Consumer 消費者

package com.zjx.thread;
/**
 * 生產者和消費者 
 * 一對一
 * 一個生產者 一個消費者
 * @author zjx
 *
 */
public class TesCommunication1 {
	public static void main(String[] args) {
		//創建庫存
		Clerk c = new Clerk();
		//創建生產者 消費者
		Productor p = new Productor(c);
		p.start();
		Consumer con= new Consumer(c);
		con.start();
	}
}
//消費者
class Consumer extends Thread{
	//共享資源 庫存
	private Clerk c;
		
	public Consumer(Clerk c) {
		this.c = c;
	}
	@Override
	public void run() {
		while(true){
			//消費產品
			c.get();
		}
	}
}
//生產者
class Productor extends Thread{
	//共享資源 庫存
	private Clerk c;
	
	public Productor(Clerk c) {
		this.c = c;
	}
	@Override
	public void run() {
		while(true){
			//生產產品(調用Clerk對象的save)
			c.save();	
		}
	}
	
}
//庫存
class Clerk{
	int count = 0;//產品數量 默認爲0
	/**
	 * 生產進貨
	 */
	public synchronized void save(){//默認鎖對象this 只有一份
		if (count >= 20) {//最多隻能存20件商品 庫存已滿
			//等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(10);//模擬延遲
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("生產者生產了一件產品,目前庫存爲: "+(++count));
		//只要生成了 消費者就可以消費了
		this.notify();//叫醒消費者消費
	}
	/**
	 * 消費出售
	 */
	public synchronized void get(){//默認鎖對象this 只有一份
		if (count <= 0) {//沒有庫存了需要等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(10);//模擬延遲
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("消費者消費了一件產品,目前庫存爲: "+(--count));
		//一消費了就可以告訴廠家 庫存不滿20了 可以繼續生產
		this.notify();
	}
}

在這裏插入圖片描述

多對多

notify -> notifyAll

if -> while

package com.zjx.thread;
/**
 * 生產者和消費者 
 * 多對多
 * 多個生產者 多個消費者
 * @author zjx
 *
 */
public class TesCommunication2 {
	public static void main(String[] args) {
		//創建庫存
		Clerk2 c = new Clerk2();
		//創建生產者 消費者
		Productor2 p1 = new Productor2("生產者路人甲號",c);
		p1.start();
		Productor2 p2 = new Productor2("生產者路人乙號",c);
		p2.start();
		Consumer2 con1= new Consumer2("*******消費者范冰冰",c);
		con1.start();
		Consumer2 con2= new Consumer2("*******消費者歐陽娜娜",c);
		con2.start();
		Consumer2 con3= new Consumer2("*******消費者迪麗熱巴",c);
		con3.start();
	}
}
//消費者
class Consumer2 extends Thread{
	//共享資源 庫存
	private Clerk2 c;
		
	public Consumer2(String name,Clerk2 c) {
		super(name);
		this.c = c;
	}
	@Override
	public void run() {
		while(true){
			//消費產品
			c.get();
		}
	}
}
//生產者
class Productor2 extends Thread{
	//共享資源 庫存
	private Clerk2 c;
	
	public Productor2(String name,Clerk2 c) {
		super(name);
		this.c = c;
	}
	@Override
	public void run() {
		while(true){
			//生產產品(調用Clerk對象的save)
			c.save();	
		}
	}
	
}
//庫存
class Clerk2{
	int count = 0;//產品數量 默認爲0
	/**
	 * 生產進貨
	 */
	public synchronized void save(){//默認鎖對象this 只有一份
		//使用while 會再判斷一次循環條件
		while(count >= 20) {//最多隻能存20件商品 庫存已滿
			//等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(10);//模擬延遲
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"生產了一件產品,目前庫存爲: "+(++count));
		//只要生成了 消費者就可以消費了
		this.notifyAll();//叫醒所有(但是可能喚醒其它正在等待的所有線程,包含生產者)
	}
	/**
	 * 消費出售
	 */
	public synchronized void get(){//默認鎖對象this 只有一份
		//使用while 會再判斷一次循環條件
		while (count <= 0) {//沒有庫存了需要等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(10);//模擬延遲
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"消費了一件產品,目前庫存爲: "+(--count));
		//一消費了就可以告訴廠家 庫存不滿20了 可以繼續生產
		this.notifyAll();//叫醒所有(但是可能喚醒其它正在等待的所有線程,包含消費者)
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章