Question
Implement your solution in Java, usingLocks andConditions. I.e., implement a LockResourceManagerclass that usesLocks andConditions to implement the request Resource (intpriority) and releaseResource ()methods described above.
Implement your solution in Java, usingLocks andConditions. I.e., implement a LockResourceManagerclass that usesLocks andConditions to implement the request Resource (intpriority) and releaseResource ()methods described above. Use the code provided to test your implementation, and try some tests of your own.
//basicresourcemanager.java//
package resourceManager; import java.util.Random;
/** * Provides almost all of the functionality required for {@link ResourceManager}s. *
Each resource manger manages a single * {@link Resource}, which may be used by any number of {@link ResourceUser}s. * Only one resource user may access the resource at any one time. Resource users request access to the * resource with a given priority. If the resource is currently in use, the resource user must wait until the resource * is no longer in use. When a resource user releases the resource the resource manager must, if there are any * resource users currently waiting to access the resource, determine what the highest priority is at which resource * users are waiting and allow one of these highest priority users to access the resource. *
** A resource can only be used a certain number of times, which is determined when its ResourceManager is * constructed. Once the resource has been used this number of times it is exhausted, and may not be used any more. *
** The only methods specified in the {@link ResourceManager} interface that are not implemented here are * {@link ResourceManager#requestResource(int)}} and {@link ResourceManager#releaseResource()}.
*/
public abstract class BasicResourceManager implements ResourceManager { // The resource this resource manager is managing. private Resource resource; // The maximum priority with which the resource can be requested. Valid priorities are in the interval [0,MAX_PRIORITY] private static final int MAX_PRIORITY = 10; /** * The number of priority levels. */ public static final int NO_OF_PRIORITIES = MAX_PRIORITY+1; // Used to keep track of the number of resource users waiting at each priority level. private int numberWaiting[] = new int[NO_OF_PRIORITIES]; /** * This value should be returned by the releaseResource() method if no waiting resource user can be found. */ public static final int NONE_WAITING = -1; // Used for generating random priority levels private Random random = new Random(System.currentTimeMillis()); private int numberOfUsers; private int usesLeft; public BasicResourceManager(Resource resource,int maxUses) { this.resource = resource; for (int priority = 0; priority < NO_OF_PRIORITIES; priority++) { numberWaiting[priority] = 0; } numberOfUsers = 0; usesLeft = random.nextInt(maxUses)+1; } public String getResourceName() { return resource.toString(); } /** * Note an increase, by one, in the number of processes waiting with a given priority. * @param priority the priority for which the increase should be noted. * @return the new number of users of the given priority noted as waiting */ public int increaseNumberWaiting(int priority) { numberWaiting[priority]++; return numberWaiting[priority]; } public int decreaseNumberWaiting(int priority) { numberWaiting[priority]--; return numberWaiting[priority]; } /** * Get the number of users of a given priority noted as waiting. * @param priority the priority of which the number of users noted as waiting is required. * @return the number of users of a given priority noted as waiting. */ public int getNumberWaiting(int priority) { return numberWaiting[priority]; } /** * Generate a random priority in the permitted range. * @return a random priority from the interval [0,MAX_PRIORITY]. */ public int getRandomPriority() { return random.nextInt(MAX_PRIORITY+1); }
/** * Check whether the resource is exhausted. * @return true iff the resource is exhausted. */ public boolean resourceIsExhausted() { return usesLeft <= 0; } public void useResource(int timeRequired) throws ResourceError { numberOfUsers++; if (numberOfUsers > 1) { throw new ResourceError(((ResourceUser) Thread.currentThread()) + " cannot use " + resource + " because it is already in use by another user"); } if (resourceIsExhausted()) { System.out.println(((ResourceUser) Thread.currentThread()) + " cannot use " + resource + " as the resource is exhausted"); } else { resource.use(timeRequired); usesLeft--; } System.out.println(resource + " has " + usesLeft + " uses left"); numberOfUsers--; } }
//resourceusers///
package resourceManager; import java.util.Set; import java.util.List; import java.util.ArrayList; import java.util.Random;
public class ResourceUser extends Thread { // The managers of the resources that this resource user wishes to use. A resource // user will terminate once all resource managers have terminated (because their // resources are no longer available. The resource managers need to be stored in // a list rather than a set in order to allow random access. private List
public ResourceUser(String name,double maxTimeRequired,Set
//resourcesystem.java//
package resourceManager; import java.util.Set; import java.util.HashSet;
public class ResourceSystem { // The set of managers managing the resources to be used by the resource users private Set
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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