生产者消费者模式--使用java信号量实现容量为1的队列

本文介绍了一种使用Java中的信号量Semaphore实现一个多线程场景的方法。在这个场景中,一个盘子只能放置一个水果,爸爸生产苹果,妈妈生产橘子,儿子消费苹果,女儿消费橘子。通过两个信号量分别控制盘子的空与满状态。

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

题目:有一个盘子,盘子只能放一个水果,爸爸每次生产一个苹果,妈妈每次生产一个橘子,儿子每次消费一个苹果,女儿每次消费一个橘子,使用信号量实现;

分析:因为盘子是有容量的,一个信号量并不能保证盘子的容量是否超过一个(除非重复轮训盘子数量,put时若盘子数量不等于0则wait,直到盘子数量等于0打破轮训),这里通过使用两个信号量notEmpty,notFull分别表示盘子是否为空,是否已满来实现;

Plate类:

package proandconusesemaphore;

import java.util.concurrent.Semaphore;

import proandconusesync.Apple;
import proandconusesync.Fruit;
import proandconusesync.Orange;

public class Plate {
	Semaphore notFull = new Semaphore(1);
	Semaphore notEmpty = new Semaphore(0);
	// Semaphore mutex = new Semaphore(1);
	Fruit fruit;

	public void putApple(Apple apple) {
		try {
			notFull.acquire();
			this.fruit = apple;
			System.out.println("爸爸放了一个苹果:  " + apple + "  " + apple.id);
			notEmpty.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void putOrange(Orange orange) {
		try {
			notFull.acquire();
			this.fruit = orange;
			System.out.println("妈妈放了一个橘子:  " + orange + "  " + orange.id);
			notEmpty.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public Apple getApple() {
		try {
			notEmpty.acquire();
			if (this.fruit instanceof Apple) {
				Apple apple = (Apple) this.fruit;
				System.out.println("儿子吃了一个苹果:  " + apple + "  " + apple.id);
				this.fruit = null;
				notFull.release();
				return apple;
			} else {
				notEmpty.release();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Orange getOrange() {
		try {
			notEmpty.acquire();
			if (this.fruit instanceof Orange) {
				Orange orange = (Orange) this.fruit;
				this.fruit = null;
				System.out.println("女儿吃了一个橘子:  " + orange + "  " + orange.id);
				notFull.release();
				return orange;
			}else{
				notEmpty.release();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
}

Main类:

package proandconusesemaphore;

import java.util.concurrent.atomic.AtomicInteger;

import proandconusesync.Apple;
import proandconusesync.Orange;

public class Main {
	public static Plate plate = new Plate();
	public static AtomicInteger id = new AtomicInteger(0);

	public static void main(String[] args) {

		new Thread(new Runnable() {

			public void run() {
				while (true) {
					Apple apple = new Apple(id.getAndIncrement());
					plate.putApple(apple);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();

		new Thread(new Runnable() {

			public void run() {
				while (true) {
					Orange orange = new Orange(id.getAndIncrement());
					plate.putOrange(orange);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				while (true) {
					plate.getApple();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
					}
				}
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				while (true) {
					plate.getOrange();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
					}
				}
			}
		}).start();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值