Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 managers; // Used to determine the time for which this resource user requires a resource private Random random = new Random(System.currentTimeMillis()); // The maximum length of time for which this resource user will require any resource. private int maxTimeRequiredMillis; // The current priority at which the resource user is currently operating. // The default value is the lowest priority - i.e. 0 private int priority = 0;

public ResourceUser(String name,double maxTimeRequired,Set managers) { setName(name); maxTimeRequiredMillis = (int) (maxTimeRequired*1000); this.managers = new ArrayList(managers); } public boolean isActive() { return managers.size() > 0; } /** * Construct a string containing this resource user's name and information about its current priority level. */ public String toString() { return "Process \"" + getName() + "\" (priority: " + priority + ")"; } public void run() { while (isActive()) { ResourceManager manager; do { manager = managers.get(random.nextInt(managers.size())); // select a resource if (manager.resourceIsExhausted()) { // is the resource still available? managers.remove(manager); // if not remove the resource manager from the list } } while (manager.resourceIsExhausted() && isActive()); // repeat until an active resource is found, or all resources are exhausted if (isActive()) { // check again if still active - the last resource might have been exhausted priority = manager.getRandomPriority(); // set a random priority for this resource request try { int timeRequired = random.nextInt(maxTimeRequiredMillis)+1; // pick a length of time to request use of the resource for System.out.println(this + " is requesting " + manager.getResourceName()); manager.requestResource(priority); // request the resource - the resource manager should suspend this resource user if the resource is not available System.out.println(this + " gained access to " + manager.getResourceName()); // request successful - resource available manager.useResource(timeRequired); // use the resource int newThreadsPriority = manager.releaseResource(); // release the resource - the request returns the priority of the process, if any, woken by this request if (newThreadsPriority == BasicResourceManager.NONE_WAITING) { System.out.println(this + " released " + manager.getResourceName() + ", there were no waiting processes"); } else { System.out.println(this + " released " + manager.getResourceName() + ", to a process with priority " + newThreadsPriority); } } catch (ResourceError error) { System.out.println("*" + error.getMessage()); } } try { sleep(random.nextInt(maxTimeRequiredMillis)+1); // pause } catch (InterruptedException ie) {} } } }

//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 managers; // The set of resource users private Set users; public ResourceSystem() { managers = new HashSet(); users = new HashSet(); } public void addResource(String name,int maxUseages) { managers.add(new ResourceManager(new Resource(name),maxUseages)); } public void addUser(String name,double maxDelay) { users.add(new ResourceUser(name,maxDelay,managers)); } public void run() throws ResourceError { for (ResourceUser user: users) { System.out.println("Starting " + user); user.start(); } try { for (ResourceUser user: users) { user.join(); System.out.println(user + " has finished"); } } catch (InterruptedException ie) { throw new ResourceError("The system was interrupted while waiting for the resource users to terminate. " + ie.getMessage()); } System.out.println("All processes finished"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Infrastructure For Medical Research In Databases

Authors: Thomas Heinis ,Anastasia Ailamaki

1st Edition

1680833480, 978-1680833485

More Books

Students also viewed these Databases questions

Question

Which of Freuds ideas did his followers accept or reject?

Answered: 1 week ago