`
fanrey
  • 浏览: 252252 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java synchronized实例 -- Hungry Bird

    博客分类:
  • JAVA
 
阅读更多
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();
}

}


}



}
}
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics