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 who 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.
Represent the birds as concurrent threads (i.e. array of "baby bird" threads and a "parent bird" thread), and the dish as a concurrent object (a monitor) that can be accessed by at most one bird at a time.
Develop a monitor (with condition variables) to synchronize the actions of the birds, i.e. develop a monitor that represents the dish. Define the monitor's operations and their implementation. Implement a multithreaded application in Java to simulate the actions of the birds represented as concurrent threads and the dish represented as as the monitor. Is your solution fair? Explain in comments in the source code.
import java.util.ArrayList;
import java.util.List;
public class HungryBirds2
{
public static List<String> wormBuffer = new ArrayList<String>();
public static int WORM_NUMBER = 10;
public static int BIRD_NUMBER = 5;
public static Object birdLock = new Object();
public static Object wormLock = new Object();
public static void main(String[] args)
{
FatherBird producer = new FatherBird();
producer.start();
for ( int i=0; i<BIRD_NUMBER; i++ )
{
BabyBird consumer = new BabyBird(i);
consumer.start();
}
}
}
class MotherBird extends Thread
{
public MotherBird()
{
}
public void run()
{
while (true)
{
synchronized(HungryBirds2.wormLock){
List<String> buffer = HungryBirds2.wormBuffer;
if(buffer.isEmpty()){
for(int i=0; i<HungryBirds2.WORM_NUMBER; i++){
String worm = "Worm " + i;
buffer.add( worm );
}
System.out.println("Parent bird produced worms! Worm number is " + buffer.size() + "!");
}
synchronized(HungryBirds2.birdLock){
try {
HungryBirds2.birdLock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try
{
Thread.sleep( 1000 );
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class KidBird extends Thread
{
private int id;
public KidBird( int id )
{
this.id = id;
}
public void run()
{
String worm = null;
while (true)
{
synchronized(HungryBirds2.wormLock){
List<String> buffer = HungryBirds2.wormBuffer;
int count = buffer.size();
if(count == 0){
synchronized(HungryBirds2.birdLock){
HungryBirds2.birdLock.notify();
}
}
else{
worm = buffer.get( count-1 );
buffer.remove( count-1 );
System.out.println("Bird " + id + " ate one worm:" + worm +"!");
try
{
Thread.sleep( 1 );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
}
分享到:
相关推荐
Java并发编程---synchronized关键
Synchronized原理-思维导图
java面试题_java-interview-questions-master.zip2、在 Java 程序中怎么保证多线程的运行安全? 出现线程安全问题的原因一般都是三个原因: 1、 线程切换带来的原子性问题 解决办法:使用多线程之间同步...
线程同步(synchronized)1---马克-to-win java视频 java视频
有需要2014版本之前的Synchronized 6-Pulse Generator元器件,提供slx文件下载,直接复制过去即可使用。Matlab表示:如果您的旧版型号包含 同步6脉冲发生器模块,它们将继续工作。但是,为获得最佳性能,请在新型号...
java synchronized的一些小实验,对帮助理解synchronized的使用有一定的帮助。
线程同步(synchronized)2---马克-to-win java视频的介绍
java中synchronized用法
java锁机制Synchronized java锁机制Synchronized java锁机制Synchronized java锁机制Synchronized
Java并发编程系列- synchronized;Java并发编程系列- synchronized;Java并发编程系列- synchronized;
java多线程编程核心技术synchronized实例大全,同步方法,同步语句块,类锁,对象锁全都用代码来展现出来
Java语言提供了专门机制以解决这种冲突,有效避免了同一个数据对象被多个线程同时访问。 需要明确的几个问题: synchronized关键字可以作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句...
主要介绍了java synchronized加载加锁-线程可重入详解及实例代码的相关资料,需要的朋友可以参考下
测试java中synchronized到底锁住了什么,完整demo
java同步synchronized关键字用法示例
Synchronized Phasor Measurements and Their Applications
Java-synchronized详解.docx
native,transient,volatile,strictfp,CMM,synchronized,java socket,压缩与解压缩,多线程,垃圾回收算法,JVM ClassLoader,IO流,反射机制,JNDI, GUI布局管理器,JMS, Java Mail, JNDI reference,java事件处理...
java里面synchronized用法
javasynchronized详解.pdf