User Tools

Site Tools


discussion

This is an old revision of the document!


Discussion

Comments, remarks, suggestions, corrections etcetera can be posted here.

Slawomir's Sleeping Barber

Please download the SleepingBarber.jar (the source code is in the jar), then run:

java -jar SleepingBarber.jar

The main logic is in the run() method of the Barber class:

public class Barber extends Thread
	{
	...
	public void run( )
		{
		try
			{
			while ( true )
				{
				synchronized ( shop )
					{
					while ( !shop.isCustomerWaiting( ) )
						shop.wait( );
					if ( isAsleep )
						{
						isAsleep = false;
						setMessage( "awake ...", "wakes up" );
						shop.isCuttingChairBusy( false );
						setMessage( "ready ...", "frees his chair" );
						}
					shop.notifyAll( );
 
					setMessage( "working ...", "seats a customer" );
					while ( !shop.isCuttingChairBusy( ) )
						shop.wait( );
					}
 
				Customer customer = shop.getCurrentCustommer( );
				setMessage( "working ...", "starts " + customer );
				sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_HAIRCUT ) );
				setMessage( "working ...", "finishes " + customer );
				setMessage( "working ...", "--------------------" );
 
				synchronized ( shop )
					{
					shop.letCurrentCustomerOut( );
					shop.notifyAll( );
					while ( shop.isCuttingChairBusy( ) )
						shop.wait( );
					shop.currentCustomerDone( );
					if ( !shop.isCustomerWaiting( ) )
						{
						shop.isCuttingChairBusy( true );
						setMessage( "sleeping ...", "takes his chair" );
						isAsleep = true;
						setMessage( "sleeping ...", "--------------------" );
						setMessage( "sleeping ...", "goes to sleep" );
						setMessage( "sleeping ...", "--------------------" );
						}
					}
				}
			}
		catch ( InterruptedException e )
			{}
		}
	...
	}

and the run() method of the Customer class:

public class Customer extends Thread
	{
	...
	public void run( )
		{
		try
			{
			while ( true )
				{
				setMessage( "away ...", "--------------------" );
				setMessage( "away ...", "is away" );
				setMessage( "away ...", "--------------------" );
				sleep( SleepingBarber.getRandomInt( SleepingBarber.MAX_TIME_AWAY ) );
 
				synchronized ( shop )
					{
					setMessage( "entering ...", "enters the shop" );
					int number = shop.getNumber( );
					if ( number != Shop.NO_ROOM )
						{
						if ( shop.isBarberAsleep( ) )
							{
							setMessage( "waiting ...", "wakes the barber" );
							shop.notifyAll( );
							}
						if ( number != shop.nextServing( ) )
							setMessage( "waiting ...", "gets a chair" );
						else
							setMessage( "waiting ...", "stands by" );
						while ( shop.isBarberAsleep( ) || shop.isCuttingChairBusy( ) || !shop.isCurrentCustomerDone( )
								|| number != shop.nextServing( ) )
							shop.wait( );
 
						setMessage( "serviced ...", "in cutting chair" );
						shop.letCurrentCustomerIn( this );
						shop.notifyAll( );
 
						setMessage( "serviced ...", "service started" );
						while ( !shop.isCuttingDone( ) )
							shop.wait( );
						setMessage( "serviced ...", "service done" );
						shop.isCuttingChairBusy( false );
						shop.notifyAll( );
						setMessage( "done ...", "out cutting chair" );
						}
					}
				}
 
			}
		catch ( InterruptedException e1 )
			{}
		}
	...
	}
discussion.1193692704.txt.gz · Last modified: 2007/10/29 21:18 by cs253184

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki