Working with Threads. Thinking in Java Exercise

P

Progzmaster

Hello it's me again. There is a exercise in this book chapter Concurrency:

Modify Restaurant.java so that there are multiple WaitPersons,
and indicate which one gets each Order. Note
that you must use notifyAll( ) instead of notify( ) in this case.

Here at the end of the page is exercise 11
http://binboy.sphere.pl/index.php?show=serwis&d=tijava&s=TIJ315.htm#Heading18442Problem is with notifyAll. I know how to synchronize a single object but multiple waitpersons is something I don't know how to synchronize.I looked on google but there wasn't any usefull information for me.I modified the code so every waitperson can have his number.Then I created Chef constructor with " Waitperson [] waitpersons" parametertypebut when I tryied to synchronize waitpersons to call notifyAll the resultwas different from expected.Please help me and write a right code so I can remember this lesson for thefuture :)And here is the original file Restaurant.java:-----------------------------------------------------------------------------------------------------------------------class Order{ public static int index = 0; private int count = ++index; public Order( ) { if ( index > 10 ) { java.lang.System.out.println( "Out of food. Closing..." ); java.lang.System.exit( 0 ); } } public String toString() { return "order #" + count; }}class Waitperson extends Thread{ private Restaurant restaurant; public Waitperson( Restaurant rest ) { super( ); restaurant = rest; start( ); } public void run( ) { while( true ) { if( restaurant.order == null) { synchronized( this ) { try { wait( ); } catch( java.lang.InterruptedException ex ) { throw new RuntimeException( ); } } } System.out.println( "Waitperson got " + restaurant.order ); restaurant.order = null; } }}class Chef extends Thread{ private Restaurant restaurant; private Waitperson waitperson; public Chef( Restaurant rest , Waitperson waitperson ) { super( ); restaurant = rest; this.waitperson = waitperson; start( ); } public void run( ) { while( true ) { if( restaurant.order == null ) { restaurant.order = new Order( ); System.out.println("Order up!!"); synchronized( waitperson ) { waitperson.notify(); } } try { sleep( 100 ); } catch( java.lang.InterruptedException ex ) { throw new RuntimeException( ); } } }}public class Restaurant{ Order order; public static void main( java.lang.String [] args ) { Restaurant restaurant = new Restaurant( ); Waitperson waitperson = new Waitperson( restaurant ); Chef chef = new Chef( restaurant, waitperson ); }}
 
P

Progzmaster

I'm sorry for the code apperance. One more time original Restaurant.java I
hope i won't break
-----------------------------------------------------------------------------------------------------------
class Order
{
public static int index = 0;
private int count = ++index;

public Order( )
{
if ( index > 10 )
{
java.lang.System.out.println( "Out of food. Closing..." );
java.lang.System.exit( 0 );
}
}

public String toString()
{
return "order #" + count;
}
}

class Waitperson extends Thread
{
private Restaurant restaurant;

public Waitperson( Restaurant rest )
{
super( );
restaurant = rest;
start( );
}

public void run( )
{
while( true )
{
if( restaurant.order == null)
{
synchronized( this )
{
try
{
wait( );
}
catch( java.lang.InterruptedException ex )
{
throw new RuntimeException( );
}
}
}
System.out.println( "Waitperson got " + restaurant.order );
restaurant.order = null;
}
}
}

class Chef extends Thread
{
private Restaurant restaurant;
private Waitperson waitperson;

public Chef( Restaurant rest , Waitperson waitperson )
{
super( );
restaurant = rest;
this.waitperson = waitperson;
start( );
}

public void run( )
{
while( true )
{
if( restaurant.order == null )
{
restaurant.order = new Order( );
System.out.println("Order up!!");
synchronized( waitperson )
{
waitperson.notify();
}
}
try
{
sleep( 100 );
}
catch( java.lang.InterruptedException ex )
{
throw new RuntimeException( );
}
}

}
}

public class Restaurant
{
Order order;

public static void main( java.lang.String [] args )
{
Restaurant restaurant = new Restaurant( );
Waitperson waitperson = new Waitperson( restaurant );
Chef chef = new Chef( restaurant, waitperson );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top