I have a problem of concourrent programming that I can't get solved by myself. I have to use Java Monitors to solve it.
The problem is the following:
Deep in the Amazon jungle lives a species of birds called Cooeecooee. These remarkable birds have an unusual behavior in raising their chicks:
- Parents alternate between hunting, dropping food in the nest, and resting.
- The parents feed the chicks by depositing a certain amount (or portions) of food in the nest.
- To teach the chicks that they must eat all food that is available, parents never deposit food in the nest until all previous food has been consumed. If the nest is not empty, they wait until nest is empty before adding food.
- Note that the nest can contain at most a capacity C of food (strangely enough, parents never return with more than C portions of food from a hunt. Observations show that the parents will feed themselves with the extra food in such a manner that at most C portions of food will ever be deposited).
Meanwhile, the chicks do all what babies do:
- Sleep
- Eat (1 portion of food a time)
- Produce guano
The program simulates life of these birds, creating two parent processes, and several chick processes.
The behavior of chicks and parents thus resembles the situation described by the following pseudo-code:
process Chicks(many of them) {
repeat {
sleep();
get_food();
eat();
digest_and_poop();
}
}
process Parents(just two) {
repeat {
hunt();
deposit_food();
rest();
}
}
The simulation will be invoked with the following parameters:
- chicks: is the number of chick processes
- baby_iter: is the number of iterations a baby chick does before it reaches adulthood
- max_food_size: is the maximum portion size a parent brings back from a hunt (same as the capacity of the nest, as it turns out)
- hunting_success_rate: represents the success rate a parent has when hunting. A rate fo 50% means that a parent returns with no food.
Synchronization is done by monitors.
Can somebody help me? Because I've resolved similar problems with Semaphores but I can't figure out how Monitors works on Java.
Thank you.