使用BlockingQueue实现包饺子( 生产者做面皮,消费者拿面皮包饺子)简单使用

本文介绍了一种基于并发编程的智能面皮生产线系统,通过生产者和消费者模式实现了面皮的自动化生产与分配,以及饺子的快速制作过程。系统利用了Java并发库中的BlockingQueue来协调生产与消费进程,确保了面皮资源的有效管理和饺子的高效包制。此解决方案展示了如何在实际应用中结合计算机科学与食品工业,实现生产流程的智能化与自动化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.zghw.concurrent.BlockingQueue;

import java.util.concurrent.atomic.AtomicInteger;
/**
 * 饺子皮对象
 * @author zghw
 *
 */
public class Skin {
	//序号
	private int num;
	//面皮厚度
	private double thick;

	public Skin(AtomicInteger id, double thick) {
		this.num = id.get();
		this.thick=thick;
	}

	public double getThick() {
		return thick;
	}

	public void setThick(double thick) {
		this.thick = thick;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
	public String toString(){
		return "面皮"+num+" 厚度为:"+thick+"mm";
	}
}

生产者:

package com.zghw.concurrent.BlockingQueue;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 做面皮用来包饺子
 * @author zghw
 *
 */
public class DoSkin implements Runnable{
	private final BlockingQueue<Skin> skinQueue;
	private static AtomicInteger id=new AtomicInteger();
	private boolean isShutdown;
	public DoSkin(BlockingQueue<Skin> skinQueue ){
		this.skinQueue=skinQueue;
		isShutdown = false;
	}
	public void stop() {
		synchronized (this) {
			isShutdown = true;
		}
	}
	@Override
	public void run() {
		while(true){
			synchronized (this) {
				if (isShutdown) {
					break;
				}
			}
			id.getAndIncrement();
			double thick =((int)(Math.random()*1000))+0.99;
			Skin sk=new Skin(id,thick);
			System.out.println("做 "+sk);
			/*try {
				Thread.sleep((int)(Math.random()*1000)+1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			skinQueue.offer(sk);
		}
	}
	
}


消费者:

package com.zghw.concurrent.BlockingQueue;

import java.util.concurrent.BlockingQueue;

/**
 * 使用面皮包饺子
 * 
 * @author zghw
 *
 */
public class MakeDumpling {
	private final BlockingQueue<Skin> skinQueue;
	private final MakeThread makeThread;
	private boolean isShutdown;

	public MakeDumpling(BlockingQueue<Skin> skinQueue) {
		this.skinQueue = skinQueue;
		makeThread = new MakeThread();
		isShutdown = false;
	}

	public void start() {
		makeThread.start();
	}

	public void stop() {
		synchronized (this) {
			isShutdown = true;
		}
		makeThread.interrupt();
	}

	private class MakeThread extends Thread {
		@Override
		public void run() {
			while (true) {
				synchronized (this) {
					if (isShutdown) {
						break;
					}
				}
				Skin skin = skinQueue.poll();
				if (skin != null) {
					/*try {
						// Thread.sleep((int) (Math.random() * 1000) + 1);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
					System.out.println("使用 " + skin + " ,包饺子");
				}
			}
		}

	}

}

package com.zghw.concurrent.BlockingQueue;

import java.util.concurrent.*;

public class MakeDumplings {

	public static void main(String[] args) {
		BlockingQueue<Skin> queue = new ArrayBlockingQueue<Skin>(100);
		ExecutorService exc = Executors.newFixedThreadPool(20);
		MakeDumpling md=new MakeDumpling(queue);
		DoSkin ds=new DoSkin(queue);
		md.start();
		exc.execute(ds);
		try {
			Thread.sleep(10000);
			ds.stop();
			md.stop();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值