Question
After reading the webpage, modify the code BakerThreadTest. Modify the code as follows: The Baker should bake the pies randomly. Use a random number generator
After reading the webpage, modify the code BakerThreadTest.
Modify the code as follows:
The Baker should bake the pies randomly. Use a random number generator to choose whetherthe next pie baked is cherry or apple.After 4 apple pies are made the Baker will only make cherry. Also, after 4 cherry pies are made the Bakerwill only make apple. Your code shouldinclude the logic for this. Besidesthese restrictions the choice of what type of pie to make should be random.
Create a CherryEater that eatsonly cherry pies. Create a AppleEaterthat eats only apple pies. Both types ofeaters wait until 2 types of their favorite pie is made and then eat 2 pies ata time after which they wait until 2 more pies are finished. After each eater eats 4 pies they arefinished.
You will need two different synchronizationobjects one for cherry and one for apple, and two different sets of methodsfor adding to and eating pies.Alternatively, you can create two different Data classes one forcherry and one for apple.
Run your program. Your program must run successfully to receive full credit for this homework.
public class TestThread {
public static void main(String args[]) {
Baker baker = new Baker( "The Baker");
baker.start();
Eater Eater = new Eater( "The Hungry Customer");
Eater.start();
}
}
public class Data {
static int num_loafs = 0;
static Object o = new Object ();
static void Add_Loaf () {
synchronized (o) {
num_loafs++;
System.out.println ("A new loaf is ready");
}
}
static void Eat_All_Loafs () {
synchronized (o) {
num_loafs = 0;
System.out.println ("Eating all loafs");
}
}
}
class Baker implements Runnable {
private Thread t;
private String threadName;
Baker( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
int i = 0;
while (i<10) {
if (Data.num_loafs < 5) {
Data.Add_Loaf ();
i++;
}
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
class Eater implements Runnable {
private Thread t;
private String threadName;
Eater( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
int num_eaten = 0;
while (num_eaten < 10) {
if (Data.num_loafs == 5) {
Data.Eat_All_Loafs();
num_eaten += 5;
}
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started