Question
The Rooms class manages a collection of rooms , indexed from 0 to m 1 ( m is an argument to the constructor). Threads
The Rooms class manages a collection of rooms, indexed from 0 to m − 1 (m is an argument to the constructor). Threads can enter or exit any room in that range. Each room can hold an arbitrary number of threads simultaneously, but only one room can be occupied at a time. For example, if there are two rooms, indexed 0 and 1, then any number of threads might enter room 0, but no thread can enter room 1 while room 0 is occupied. Fig. 8.13 shows an outline of the Rooms class.
Each room can be assigned an exit handler: Calling setExitHandler(i, h) sets the exit handler for room i to handler h. The exit handler is called by the last thread to
FIGURE 8.13
The Rooms class.
leave a room, but before any threads subsequently enter any room. This method is called once per room and while it is running, no threads are in any rooms.
Implement the Rooms class. Make sure that:
• If some thread is in room i, then no thread is in room j ≠= i.
• The last thread to leave a room calls the room’s exit handler, and no threads are in any room while that handler is running.
• Your implementation is fair: Any thread that tries to enter a room eventually succeeds. (You may assume that every thread that enters a room eventually leaves.)
public class Rooms ( public interface Handler { void onEmpty(); public Rooms (1nt m) ... }; public votd enter(1nt i) { ... }; public boolean exit () (... ); public void setExitHandler (1nt i, Rooms.Handler h) ... };
Step by Step Solution
3.40 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Problem arise if a push runs concurrently with a pop Suppose p...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started