Question: THESE ARE THE CODE GIVEN Baker.java class Baker implements Runnable { private Thread t; private String threadName; Baker( String name){ threadName = name; System.out.println(Creating

 THESE ARE THE CODE GIVEN Baker.java class Baker implements Runnable {

THESE ARE THE CODE GIVEN

Baker.java

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

Data.java

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"); } } }

Eater.java

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

TestThread

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(); } }

After reading the webpage, modity the code BakerThreadTest. Modify the code as follows: 1. 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. 2. 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. 3. 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!