Results 1 to 8 of 8
- 03-16-2013, 04:34 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 7
- Rep Power
- 0
My simulation for concurrent processing didn't work
I am suppose to write a program to simulate this situation.
A tribe of savages eats communal dinners from a large pot that can hold M servings of stewed missionaries. When a savage wants to eat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage wakes up the cook and then waits until the cook has refilled the pot.
Admin class: This class creates the cook and generates arrival of 10 savages. The class also keeps track of the number of servings in the pot (using an array of size 1) and prints the number of servings left in the pot after all the savages have eaten.
Cook class: This class models the cooking done by the cook.
It should have a method putServingsInPot() that performs the following:
- generate a random number M (between 1 and 5 inclusive)
- show a message the cook has put M missionaries into the pot.
- include a timing delay to represent the cooking relative to M.
This method is invoked only when the pot is empty.
Savage class: This class models the behaviour of the savages.
It should have a suitable method that performs the following:
get a serving from the pot
- include a timing delay to represent the time spent eating
- deduct one serving from the pot
- show messages that the savage starts eating and has finished eating
When getting a serving from the pot, if the pot is empty, the cook’s method putServingsInPot() needs to be invoked.
These are my codes but the output is totally wrong. But I just can't understand where goes wrongPlease help!
Java Code:public class Admin { public static void main (String[] args) { Cook cook = new Cook(); Savage[] savage = new Savage[10]; int[] numOfServings = new int[1]; for (int i = 0; i<savage.length; i++) { savage[i] = new Savage(i+1, numOfServings, cook); savage[i].start(); } System.out.println("ending...There are " + numOfServings[0] + " missionaries left in the pot"); } }
Java Code:public class Cook extends Thread { public synchronized void putServingsInPot(int[] numOfServings) { int m = (int)(Math.random()*5)+1; Timer.cook(m); numOfServings[0]+=m; System.out.println("Cook has put " + m + " missionaries into pot."); notifyAll(); } }
Java Code:public class Savage extends Thread { private int id; private int[] numOfServings; private Cook c; public Savage(int id, int[] numOfServings, Cook c) { this.id=id; this.numOfServings = numOfServings; this.c=c; } public void run() { for(int i=0; i<2; i++) { getServingFromPot(); } } public synchronized void getServingFromPot() { try { while(numOfServings[0]==0) { c.putServingsInPot(numOfServings); wait(); } System.out.println("Salvage " + id + " getting a serving to eat"); Timer.eat(id); numOfServings[0]--; System.out.println("Salvage " + id + " finished eating"); } catch (Exception e) { } } }
Java Code:public class Timer { public static void eat(int time) { duration ((time+10)*2); } public static void cook (int share) { duration (share*100); } public static void duration(int time) { int speedFactor = 1000; for(int j = 0; j<time; j++) { for(int k = 0; k<speedFactor; k++) { double r = (k/2)*2; } } } }
Cook has put 5 missionaries into pot
Savage 0 getting a serving to eat
Savage 3 getting a serving to eat
Savage 5 getting a serving to eat
Savage 1 getting a serving to eat
Savage 9 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 2 missionaries into pot
Savage 5 finished eating
Savage 3 finished eating
Savage 0 finished eating
Savage 1 finished eating
Savage 9 finished eating
Savage 7 getting a serving to eat
Savage 8 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 5 missionaries into pot
Savage 8 finished eating
Savage 7 finished eating
Savage 6 getting a serving to eat
Savage 4 getting a serving to eat
Savage 2 getting a serving to eat
Savage 4 finished eating
Savage 6 finished eating
Savage 2 finished eating
There are 2 missionaries in the pot!
- 03-16-2013, 06:25 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: My simulation for concurrent processing didn't work
You showed what the output is supposed to be but not what you are producing. Can you include that please?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-16-2013, 06:39 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: My simulation for concurrent processing didn't work
Well, I got it to compile and it isn't working as expected. However, I did notice that you didn't start your cook thread. In fact, you didn't override the run() method in your thread even though you subclassed Thread. Also, there was at least one try/catch that ignored the exception. At a minimum you should print the stack trace.
Regards,
JimLast edited by jim829; 03-16-2013 at 06:55 PM. Reason: found other problems
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-17-2013, 04:53 AM #4
Member
- Join Date
- Mar 2013
- Posts
- 7
- Rep Power
- 0
Re: My simulation for concurrent processing didn't work
My output is totally wrong:
Cook has put 2 missionaries into pot.
ending...There are 2 missionaries left in the pot
Salvage 9 getting a serving to eat
Salvage 9 finished eating
Salvage 9 getting a serving to eat
Salvage 9 finished eating
Salvage 8 getting a serving to eat
Cook has put 2 missionaries into pot.
Salvage 8 finished eating
Salvage 8 getting a serving to eat
Cook has put 3 missionaries into pot.
Salvage 8 finished eating
Cook has put 4 missionaries into pot.
Cook has put 5 missionaries into pot.
Cook has put 1 missionaries into pot.
Cook has put 5 missionaries into pot.
Cook has put 3 missionaries into pot.
- 03-17-2013, 05:28 AM #5
Member
- Join Date
- Mar 2013
- Posts
- 7
- Rep Power
- 0
Re: My simulation for concurrent processing didn't work
The cook thread should start only when the pot is empty. I rewrite my classes here, but still it doesn't work.
Java Code:public class Cook extends Thread { private int[] numOfServings; public Cook(int[] numOfServings) { this.numOfServings = numOfServings; } public void run() { putServingsInPot(numOfServings); } private synchronized void putServingsInPot(int[] numOfServings) { int m = (int)(Math.random()*5)+1; Timer.cook(m); numOfServings[0]+=m; System.out.println("Cook has put " + m + " missionaries into pot."); notifyAll(); } }
Java Code:public class Savage extends Thread { private int id; private int[] numOfServings; private Cook c; public Savage(int id, int[] numOfServings, Cook c) { this.id=id; this.numOfServings = numOfServings; this.c=c; } public void run() { for(int i=0; i<2; i++) { getServingFromPot(); } } private synchronized void getServingFromPot() { while(numOfServings[0]==0) { c.start(); try{ wait(); } catch (Exception e){} } //out of while means cook has put missionaries into pot System.out.println("Salvage " + id + " getting a serving to eat"); Timer.eat(id); numOfServings[0]--; System.out.println("Salvage " + id + " finished eating"); } }
Java Code:public class Admin { public static void main (String[] args) { int[] numOfServings = new int[1]; Cook cook = new Cook(numOfServings); Savage[] savage = new Savage[10]; for (int i = 0; i<savage.length; i++) { savage[i] = new Savage(i+1, numOfServings, cook); savage[i].start(); } System.out.println("ending...There are " + numOfServings[0] + " missionaries left in the pot"); } }
- 03-17-2013, 02:18 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: My simulation for concurrent processing didn't work
The cook method should be started at the beginning. In the run method, you need a synchronized block with a wait statement. Something like this (but additional code will probably be necessary. Like letting the Savages know the pot has been updated).
Java Code:synchronized(lock) { while (potContents > 0) {// non-empty pot lock.wait(); updatePot(); // must update potContents or loop will exit } }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-17-2013, 04:38 PM #7
Member
- Join Date
- Mar 2013
- Posts
- 7
- Rep Power
- 0
My wait() and notify() wouldnt work.
I modified my codes as below and start my Cook thread in Admin. But still the result is not what I expect
Java Code:public class Cook extends Thread { private int[] numOfServings; public Cook (int[] numOfServings) { this.numOfServings = numOfServings; } public void run() { putServingsInPot(numOfServings); } public synchronized void putServingsInPot (int[] numOfServings) { int m = (int)(Math.random()*5)+1; Timer.cook(m); numOfServings[0]+=m; System.out.println("Cook has put " + m + " missionaries into pot."); notifyAll(); } }
Java Code:public class Savage extends Thread { private int[] numOfServings; private int id; private Cook c; public Savage(int[] numOfServings, int id, Cook c) { this.numOfServings=numOfServings; this.id=id; this.c=c; } public void run() { for(int i = 0; i<2; i++) { getServingsFromPot(id, numOfServings); } } public synchronized void getServingsFromPot(int id, int[] numOfServings) { try{ while(numOfServings[0]==0) { System.out.println("There's no more missionary in the pot!"); c.putServingsInPot(numOfServings); wait(); } } catch(Exception e){} System.out.println("Savage " + id + " getting a serving to eat"); Timer.eat(id); numOfServings[0]--; System.out.println("Savage " + id + " finished eating"); } }
Java Code:public class Admin { public static void main (String[] args) { int[] numOfServings = new int[1]; Cook cook = new Cook(numOfServings); Savage[] savage = new Savage[10]; cook.start(); for(int i = 0; i<savage.length; i++) { savage[i] = new Savage(numOfServings, i+1, cook); savage[i].start(); } System.out.println("Ending...There are " + numOfServings[0] + " missionarie left in the pot"); } }
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
There's no more missionary in the pot!
Cook has put 3 missionaries into pot.
Cook has put 1 missionaries into pot.
Cook has put 5 missionaries into pot.
Cook has put 3 missionaries into pot.
Cook has put 5 missionaries into pot.
Cook has put 3 missionaries into pot.
Cook has put 4 missionaries into pot.
Cook has put 2 missionaries into pot.
Cook has put 3 missionaries into pot.
Cook has put 2 missionaries into pot.
Cook has put 1 missionaries into pot.
- 03-17-2013, 04:53 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: My wait() and notify() wouldnt work.
Did you read thru the tutorials and try out some examples.? Take some small steps first to see how threads behave. Concurrent programming ins one of the more complex (and difficult to debug) aspects of programming.
Also, please don't change the title when posting replies.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Please help me out with these errors? I've tried to work it out, but it didn't work
By almaty in forum New To JavaReplies: 2Last Post: 11-25-2012, 03:51 AM -
jbuttons and jcombobox didn't show up please help...
By jackwong in forum New To JavaReplies: 3Last Post: 06-08-2012, 05:48 PM -
The code didn't delete the text file
By amyng in forum New To JavaReplies: 3Last Post: 05-01-2012, 07:37 AM -
class contains constructor with one variable, driver code didn't work, ask for help!
By myloveca in forum New To JavaReplies: 0Last Post: 02-27-2012, 11:29 PM -
My own ClassLoader didn't work.
By snooze-g in forum Advanced JavaReplies: 1Last Post: 07-17-2007, 12:12 PM
Bookmarks