0 évaluation0% ont trouvé ce document utile (0 vote)
43 vues3 pages
The document describes a programming assignment to simulate a politically correct unisex bathroom using threads and synchronization. A Bathroom class would manage access and track state using synchronized methods. MaleThread and FemaleThread classes would contain threads that enter and exit the bathroom by calling methods on the shared Bathroom object. The main program would create the Bathroom, threads, and display output from the simulation.
The document describes a programming assignment to simulate a politically correct unisex bathroom using threads and synchronization. A Bathroom class would manage access and track state using synchronized methods. MaleThread and FemaleThread classes would contain threads that enter and exit the bathroom by calling methods on the shared Bathroom object. The main program would create the Bathroom, threads, and display output from the simulation.
The document describes a programming assignment to simulate a politically correct unisex bathroom using threads and synchronization. A Bathroom class would manage access and track state using synchronized methods. MaleThread and FemaleThread classes would contain threads that enter and exit the bathroom by calling methods on the shared Bathroom object. The main program would create the Bathroom, threads, and display output from the simulation.
Whenever we have multiple entities working together to get something done we have the pro!lem o" synchronization !etween the entities# $rocesses working together or threads in one or many processes working together need tools to protect critical sections and to signal one another when important events occur# %ava provides thread and synchronization mechanisms which programmers can use to coordinate their activities# The "ollowing pro!lem re&uires that many threads coordinate their activities to assure that the program runs correctly# ' university wants to demonstrate its political correctness !y applying the Supreme court(s )Separate !ut e&ual is inherently une&ual* doctrine to gender as well as race# 's such it has decided that !oth genders will use the same !athroom "acilities# +owever in order to preserve some tradition it decrees that when a woman is in the !athroom only other women may enter and when a man is in the !athroom only other men may enter# Each politically correct !athroom will have a sign on the outside indicating what state the !athroom is in, Empty Women $resent or -en $resent# .our /o! is to implement a multi-threaded simulation o" this politically correct !athroom# .ou may use whatever counters and synchronization mechanisms you need to make sure that your politically correct !athroom works according to the university decree# .our implementation must generate some status report whenever anyone enters or leaves the !athroom which should include the se0 o" the person entering or leaving# This status report should include the state o" the !athroom the num!er o" men and1or women waiting and the num!er and se0 o" the occupants currently in the !athroom# 2nce a person 3thread4 enters the !athroom that person(s stay in the !athroom should !e determined !y a random num!er# The minimum stay should !e 5 seconds and the ma0imum stay should !e 6 seconds# 2nce a person 3thread4 has le"t the !athroom use random num!ers to determine when that thread will try to gain access again# 7 suggest that the wait should range "rom 4 seconds to 80 seconds# .our program should start 6 male threads and 6 "emale threads# Each thread should visit the !athroom 80 times# 'nalyze the output to make sure things are working correctly# Capture a sample o" your program(s status reports# 'lso print out your source code# 9emonstrate your program to the pro"essor and get a sign o""# Turn in your output sample and source code, and your signed-off coversheet. Basic Program Structure for PCB (Politically Correct Bathroom) $C: Class This class really does all the work o" synchronizing access to the $C:# The "irst &uestion to ask is what in"ormation does the $C: need to keep# The $C: has a state which re"lects i" it is empty or i" there are males or "emales inside# 7t also must keep track o" the num!er o" occupants which are inside and the num!er that are waiting to get inside# There can !e males or "emales inside or waiting# The $C: must also have a means o" !locking a thread which is trying to gain access i" the state o" the $C: does not permit access "or this thread at this time# 7n order to !lock a thread the $C: can use the wait method which will !e used to !lock threads which must wait until the state o" the $C: permits access# The $C: should keep a counter which indicates how many males or "emales are !locked waiting to get into the $C:# Since the $C: has multiple mem!er varia!les which must !e read and written !y multiple threads we must synchronize access to these mem!er varia!les !y making the methods o" this class synchronized methods# The constructor "or the $C: class must initialize all mem!er varia!les including all the state and counter varia!les# There should !e a maleEnters3 4 and a "emaleEnters3 4 method# These methods check the state to see whether the male or "emale thread is allowed to enter or must !e !locked# ;emem!er reading and1or writing the mem!er varia!les must !e done inside a critical section which is protected !y the synchronized methods# State and1or counter varia!les must !e updated appropriately# 7" the male or "emale must wait to enter the wait method is used to !lock the thread# ;eturning "rom one o" these methods indicates that the thread was success"ul in entering the $C:# There should also !e a "emaleE0its3 4 and a maleE0its3 4 method# These methods update the counter and state mem!er varia!les appropriately# 7" the thread e0iting the !athroom is the last one out the state mem!er varia!le must change and any waiting threads must !e un!locked so that they can attempt to enter again# The notifyAll method can !e used to wake up all the threads which are !locked waiting to get into the $C:# ;emem!er that something in your simulation must output the state and counter in"o "re&uently or whenever any thread enters or e0its the $C:# -aleThread and <emaleThread Classes These classes contain the code that simulates the actions o" a male or "emale thread entering and e0iting the $C:# These two classes can either inherit "rom the Thread class or implement the ;unna!le inter"ace# The constructor "or these two classes should accept a $C: o!/ect so that all the male and "emale threads are using the same $C: o!/ect# The run method o" these two classes should contain a loop which e0ecuted 80 times# 7nside the loop a thread would sleep "or a random amount o" time !etween 4 and 80 seconds and then call the appropriate entry method on the $C:# 2nce that method returns the thread has gained access to the $C: and should wait "or !etween 5 and 6 seconds !e"ore calling the appropriate e0it method on the $C: o!/ect# -ain Test $rogram This program controls the simulation#The main program must create a $C: o!/ect to !e used !y the male and "emale threads# 7t must also create 6 -aleThread o!/ects and 6 <emaleThread o!/ects and start all the male and "emale threads e0ecuting# 7t is up to you how to display the output "rom your simulation# .ou can use a console window and report status every time a thread enters starts waiting stops waiting or e0its the $C: o!/ect# 2r you can use a simple =>7 and use the main thread to periodically 3every 800 msec4 update a display that shows in some way the threads that are in the $C: or that are waiting to get in and whether they are male or "emale threads#