使用concurrent 重写生产者 消费者

本文通过一个食堂馒头模拟场景,演示了如何使用Java并发编程解决资源有限条件下的生产和消费问题。模拟中,食堂仅提供10个座位,其中3个为厨师专用,剩余7个供学生使用。同时,考虑到食堂的馒头存放限制及学生用餐习惯,该模拟还涉及到了资源的合理分配与利用。

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

场景:学生来食堂吃馒头

         

         1.食堂位置有限,只有10个座位,其中3个座位是厨师大叔的,其他7个座位是学生的。学生共有100个。

         2.食堂的大框只能容下10个馒头。

         3.大叔在学生来之前会事先准备好10个馒头。

         4.大叔只做300个馒头,然后把位子让给学生吃饭。

         5.学生吃3个馒头后就饱了,把位子让给其他人。

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class Tester {

	static CountDownLatch count = new CountDownLatch(10);

	public static void main(String[] args) throws InterruptedException {
		// 食堂只有10个位子
		Executor shitang = Executors.newFixedThreadPool(10);
		// 做饭的师傅有3个人
		int shifucount = 3;
		// 吃饭的学生有100人;
		int chifancount = 100;
		// 有一个能容纳10个馒头的桶
		LinkedBlockingQueue<ManTou> tong = new LinkedBlockingQueue<ManTou>(10);
		// 师傅开始做馒头了
		for (int i = 0; i < shifucount; i++) {
			shitang.execute(new Producer(tong,chifancount));
		}
		count.await();
		System.out.println("学生开始来吃。。。。。");
		// 学生开始吃馒头
		for (int j = 0; j < chifancount; j++) {
			shitang.execute(new Consumer(tong,j));
		}
	}
}
import java.util.concurrent.LinkedBlockingQueue;

public class Producer implements Runnable {

	private LinkedBlockingQueue<ManTou> tong;
	private int chifancount;

	public Producer(LinkedBlockingQueue<ManTou> tong2, int chifancount) {
		this.tong = tong2;
		this.chifancount = chifancount;
	}

	public void run() {
		while (true) {
			try {
				if (chifancount*3 <= ManTou.mantouid.get()) {
					Thread.currentThread().interrupt();
				}
				ManTou m = new ManTou();
				tong.put(m);
				System.out.println(Thread.currentThread().getName()
						+ "********" + m.id);
				Tester.count.countDown();
				Thread.sleep(1000);

			} catch (InterruptedException e) {
				System.out.println("馒头够了,不用再生产了");
				break;
			}
		}
	}
}
import java.util.concurrent.LinkedBlockingQueue;
public class Consumer implements Runnable {
	int baole=0;
	
	private int id;
	
	private LinkedBlockingQueue<ManTou> tong;

	public Consumer(LinkedBlockingQueue<ManTou> tong) {
		this.tong = tong;
	}

	public Consumer(LinkedBlockingQueue<ManTou> tong2, int j) {
		this.tong = tong2;
		this.id=j;
	}

	public void run() {
		while(true){
			try {
				ManTou m =tong.take();
				Thread.sleep(5000);
				baole++;
				System.out.println("学生"+id+"在座位"+Thread.currentThread().getName()+"吃了馒头"+m.id);
				if(baole==3){
					Thread.currentThread().interrupt();
				}
			} catch (InterruptedException e) {
				System.out.println("学生"+id+"吃饱了");
				break;
			}
		}
           
	}
}
import java.util.concurrent.atomic.AtomicInteger;

public class ManTou {
	public static AtomicInteger  mantouid = new AtomicInteger(0);
	
	int id;

	public ManTou() {
		synchronized (mantouid) {
			this.id = mantouid.get();
			mantouid.addAndGet(1);
		}
		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值