The Hungry Birds Problem (one producer - multiple consumers)
Given are n baby birds and one parent bird. The baby birds eat out of a common dish that initially contains W worms. Each baby bird repeatedly takes a worm, eats it, sleeps for a while, takes another worm, and so on. If the dish is empty, the baby bird that discovers the empty dish chirps real loud to awaken the parent bird. The parent bird flies off and gathers W more worms, puts them in the dish, and then waits for the dish to be empty again. This pattern repeats forever.
Develop and implement a multithreaded program to simulate the actions of the birds. Represent the birds as concurrent threads (i.e. array of "babyBird" threads and a "parentBird" thread), and the dish as a critical shared resource that can be accessed by at most one bird at a time. Use only semaphores for synchronization. Your program should print a trace of interesting simulation events. Is your solution fair? Explain in comments to the source code.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
public class HungryBirds
{
public static List<String> wormBuffer = new ArrayList<String>();
public static int WORM_NUMBER = 10;
public static int BIRD_NUMBER = 5;
public static Semaphore semaphoreBufParent = new Semaphore(1);
public static Semaphore semaphoreBufChild = new Semaphore(0);
public static Semaphore semaphoreBuffer = new Semaphore(1);
public static void main(String[] args)
{
ParentBird producer = new ParentBird();
producer.start();
for ( int i=0; i<BIRD_NUMBER; i++ )
{
ChildBird consumer = new ChildBird(i);
consumer.start();
}
}
}
class ParentBird extends Thread
{
public ParentBird()
{
}
public void run()
{
while (true)
{
// try to acquire a semaphore if the buffer is not full
try
{
HungryBirds.semaphoreBufParent.acquire();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<String> buffer = HungryBirds.wormBuffer;
try {
HungryBirds.semaphoreBuffer.acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for(int i=0; i<HungryBirds.WORM_NUMBER; i++){
String worm = "Worm " + i;
buffer.add( worm );
}
System.out.println("Parent bird produced worms! Worm number is " + buffer.size() + "!");
HungryBirds.semaphoreBuffer.release();
HungryBirds.semaphoreBufChild.release();
try
{
Thread.sleep( 1000 );
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ChildBird extends Thread
{
private int id;
public ChildBird( int id )
{
this.id = id;
}
public void run()
{
String worm = null;
while (true)
{
// try to acquire a semaphore if the buffer is not empty
try
{
HungryBirds.semaphoreBufChild.acquire();
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
// this consumer thread is accessing the shared buffer...
try {
HungryBirds.semaphoreBuffer.acquire();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<String> buffer = HungryBirds.wormBuffer;
int count = buffer.size();
if(count == 0){
HungryBirds.semaphoreBufParent.release();
}
else{
worm = buffer.get( count-1 );
buffer.remove( count-1 );
System.out.println("Bird " + id + " ate one worm:" + worm +"!");
HungryBirds.semaphoreBufChild.release();
}
HungryBirds.semaphoreBuffer.release();
try
{
Thread.sleep( 1 );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
分享到:
相关推荐
Semaphore T-BOX System OverviewPDF,Semaphore T-BOX System Overview
Semaphore T-Box RTUpdf,Semaphore T-Box RTU
介绍临界资源的访问机制,有Semaphore,signal,mutex等
JAVA多线程--信号量(Semaphore)_.docx
Kubernetes的Semaphore CI / CD演示 这是一个示例应用程序和CI / CD管道,展示了如何使用Semaphore 2.0将微服务构建,测试和部署...docker run -p 80:4567 semaphore-demo-ruby-kubernetes curl localhost > hello worl
深入浅出java Semaphore
semaphore开发源码,ansible可视化部署服务程序,支持界面操作。
mutex和semaphore互斥程序实例
使用Java Spring的信号量演示CI / CD管道示例Spring Boot应用程序和CI / CD管道显示了如何在上运行Java项目。应用概述产品特点简单的登录屏幕用户注册通过Basic身份验证层保护的端点用于在数据库中存储用户的持久层...
Semaphore是计数信号量。Semaphore管理一系列许可证。每个acquire方法阻塞,直到有一个许可证可以获得然后拿走一个许可证;每个release方法增加一个许可证,这可能会释放一个阻塞的acquire方法。然而,其实并没有...
python库。 资源全名:semaphore-0.4.46.zip
在JAVA里面,我的理解是信号计数的作用,比如我想设置只能允许500个线程访问WebServer,那么如何控制500个数量哪?每来一个请求,就通过acquire()获取一个许可,然后数量自动减一,处理完成之后通过release()释放一...
使用PHP Laravel的信号量演示CI / CD管道示例应用程序和CI / CD管道显示了如何在Semaphore 2.0上运行PHP Laravel项目。本地项目设置要在本地设置项目,根据,您的本地环境需要满足常见的Laravel开发要求...semaphore-id
Google Cloud Run的信号量CI / CD演示 这是一个示例应用程序和CI / CD管道,显示了如何使用Semaphore 2.0将无服务器微服务构建,测试和部署到Google Cloud... docker build -t semaphore-demo-ruby-kubernetes . docker
semaphore-nodejs 在 node.js 中使用 API 的小型客户端库 ##用法 在你的项目中安装 npm > npm install semaphore-sms 用法示例: var semaphore = require ( 'semaphore-sms' ) ; semaphore . set_api_key ...
快速开始安装git clone https://github.com/abn/semaphore-docker-systemd.gitcd semaphore-docker-systemdsudo make install用法 # This is as simple assystemctl start semaphore# If you want semaphore to ...
Semaphore信号量;ReadWriteLock读写锁;CountDownLatch计时器;CyclicBarrier循环栅栏; 重⼊锁可以完全替代synchronized关键字。在JDK5.0的早期版本中,重⼊锁的性能远远好于 synchronized,但从JDK6.0开始,JDK在...
带你看看Java的锁-Semaphore前言简介使用源码分析类结构图SyncNonfairSyncFairSyncSemaphore 构造函数Semaphore 成员方法获取释放总结 前言 简介 Semaphore 中文称信号量,它和ReentrantLock 有所区别,...
Semaphore 源码解析 通过使用Semaphore可以在高并发场景进行限流控制处理
java buffer using Semaphore a kind of example of writer and reader