Assume that a finite number of resources of a single resource type must be managed. Processes may ask for a number of these resources and —once finished—will return them. As an example, many commercial software packages provide a given number of licenses, indicating the number of applications that may run concurrently. When the application is started, the license count is decremented. When the application is terminated, the license count is incremented. If all licenses are in use, requests to start the application are denied. Such requests will only be granted when an existing license holder terminates the application and a license is returned.
The following Java class is used to manage a finite number of instances of an available resource. Note that when a process wishes to obtain a number of resources, it invokes the decreaseCount() method. Similarly, when a process wants to return a number of resources, it calls increaseCount(). public class Manager { public static final int MAX RESOURCES = 5; private int availableResources = MAX RESOURCES; /** * Decrease availableResources by count resources. * return 0 if sufficient resources available, * otherwise return -1 */ public int decreaseCount(int count) { if (availableResources < count) return -1; else { availableResources -= count; return 0; } /* Increase availableResources by count resources. */ public void increaseCount(int count) { availableResources += count; } } However, the preceding program segment produces a race condition. Do the following
c. Using Java synchronization, fix the race condition. Also modify decreaseCount() so that a thread blocks if there aren’t sufficient resources available.
Get Answers For Free
Most questions answered within 1 hours.