Write a multithreaded simulation of a barbershop. Use a queue to share the available barbers. At the start of the program, insert barber names into the queue. Create a thread for each cus- tomer. As each customer arrives, the customer waits for an available barber, gets a haircut, returns the barber to the queue and exits the barber shop. Use random numbers and function sleep to stagger arrival

times and to simulate haircut time.

What will be an ideal response?


```
# Barber shop example.

from Queue import Queue
import threading
import random
import time

class CustomerThread( threading.Thread ):
"""Class using queues"""

def __init__( self, threadName, queue ):
"""Initialize thread"""

threading.Thread.__init__( self, name = threadName )
self.queue = queue

def run( self ):
"""Wait for barber, get haircut and exit"""

time.sleep( random.randrange( 0, 10 ) ) # stagger arrivals

# wait for available barber
print "%s arrived; waiting for haircut..." % self.getName()
barber = self.queue.get()

print " %s getting haircut from ā€™%sā€™." % \
( self.getName(), barber )

time.sleep( random.randrange( 5, 10 ) ) # get haircut

# free a barber
print " %s exiting; freeing barber ā€™%sā€™." % \
( self.getName(), barber )
self.queue.put( barber )

threads = [] # list of threads
queue = Queue()

# add barbers
queue.put( "Frank" )
queue.put( "Joe" )
queue.put( "Steve" )

# create ten threads
for i in range( 1, 11 ):
threads.append( CustomerThread( "Customer" + str( i ),
queue ) )

# start each thread
for thread in threads:
thread.start()
```
Customer3 arrived; waiting for haircut...
Customer3 getting haircut from 'Frank'.
Customer5 arrived; waiting for haircut...
Customer5 getting haircut from 'Joe'.
Customer10 arrived; waiting for haircut...
Customer10 getting haircut from 'Steve'.
Customer9 arrived; waiting for haircut...
Customer7 arrived; waiting for haircut...
Customer2 arrived; waiting for haircut...
Customer5 exiting; freeing barber 'Joe'.
Customer9 getting haircut from 'Joe'.
Customer6 arrived; waiting for haircut...
Customer8 arrived; waiting for haircut...
Customer3 exiting; freeing barber 'Frank'.
Customer7 getting haircut from 'Frank'.
Customer4 arrived; waiting for haircut...
Customer1 arrived; waiting for haircut...
Customer10 exiting; freeing barber 'Steve'.
Customer2 getting haircut from 'Steve'.
Customer7 exiting; freeing barber 'Frank'.
Customer6 getting haircut from 'Frank'.
Customer9 exiting; freeing barber 'Joe'.
Customer8 getting haircut from 'Joe'.
Customer2 exiting; freeing barber 'Steve'.
Customer4 getting haircut from 'Steve'.
Customer8 exiting; freeing barber 'Joe'.
Customer1 getting haircut from 'Joe'.
Customer6 exiting; freeing barber 'Frank'.
Customer4 exiting; freeing barber 'Steve'.
Customer1 exiting; freeing barber 'Joe'.

Computer Science & Information Technology

You might also like to view...

To cut a highlighted line from a document in Gedit, what keyboard shortcut can be used?

A. Alt+C B. Ctrl+X C. Alt+X D. Ctrl+V

Computer Science & Information Technology

The accompanying figure contains code that styles normal links, and links will be displayed in _____.

A. gray B. yellow C. blue D. red

Computer Science & Information Technology

When a client attempts to access a namespace root or the underlying folders, what type of prioritized list does it receive?

A. root B. referral C. priority D. policy

Computer Science & Information Technology

How does the GlobalNames zone (GNZ) feature work?

What will be an ideal response?

Computer Science & Information Technology