patternjavaMinor
Resource Pool implementation with ReentrantLock and Condition
Viewed 0 times
implementationconditionreentrantlockresourcewithandpool
Problem
The Resource Pool is similar to the implementation of a semaphore. It takes the class type of the resource and number of resource pools associated with it as the constructor parameters. There is a small ambiguity here, when the
```
package concurrency;
import java.util.Hashtable;
import java.util.Vector;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
interface ResourceA {
String getName();
}
interface ResourceExceptionHandler {
void handle(Exception exc);
}
class ResourceExceptionHandlerImpl implements ResourceExceptionHandler {
@Override
public void handle(Exception exc) {
}
}
class ResourceCreationException extends Exception {
}
public class ResourcePool {
private final Lock lock = new ReentrantLock();
private final Condition poolAvailable = lock.newCondition();
private int num_resource_pools;
private final Hashtable> resource_pool_table = new Hashtable>();
private final Class resourceClass;
private final ResourceExceptionHandler resourceExceptionHandler = new ResourceExceptionHandlerImpl();
private R createResource(String str) {
R resource = null;
try {
resource = resourceClass.getDeclaredConstructor(String.class)
.newInstance(str);
if (resource == null)
throw new ResourceCreationException();
} catch (Exception e) {
reso
Resource instance is created it uses reflection, but when we require the resource name a call to getName() from the ResourceA interface is sufficient. Each Resource has a corresponding resource pool entry in the resource_pool_table Map. The poolAvailable Condition variable will signal all waiting threads when a resource is released back into the pool. Correspondingly threads will wait on the Resource Pool lock condition until an object pool is available for the resource with the resourceName. ```
package concurrency;
import java.util.Hashtable;
import java.util.Vector;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
interface ResourceA {
String getName();
}
interface ResourceExceptionHandler {
void handle(Exception exc);
}
class ResourceExceptionHandlerImpl implements ResourceExceptionHandler {
@Override
public void handle(Exception exc) {
}
}
class ResourceCreationException extends Exception {
}
public class ResourcePool {
private final Lock lock = new ReentrantLock();
private final Condition poolAvailable = lock.newCondition();
private int num_resource_pools;
private final Hashtable> resource_pool_table = new Hashtable>();
private final Class resourceClass;
private final ResourceExceptionHandler resourceExceptionHandler = new ResourceExceptionHandlerImpl();
private R createResource(String str) {
R resource = null;
try {
resource = resourceClass.getDeclaredConstructor(String.class)
.newInstance(str);
if (resource == null)
throw new ResourceCreationException();
} catch (Exception e) {
reso
Solution
-
No need to use synchronization in the constructor - at the moment of creation the object is not yet visible to any concurrent thread.
-
Declare a field
-
Rename
-
Create a couple of tests and run them every time the code is modified.
No need to use synchronization in the constructor - at the moment of creation the object is not yet visible to any concurrent thread.
-
Declare a field
Constructor resourceConstructor and in the constructor, initialize it with resourceClass.getDeclaredConstructor(String.class). Thus the possible error when there is no such constructor is determined early, and saves time to extract the constructor later. Use it in the method createResource():resource = resourceConstructor.newInstance(str);-
Rename
num_resource_pools to num_resources, as it actually counts the overall number of resources, not number of pools.-
Create a couple of tests and run them every time the code is modified.
Code Snippets
resource = resourceConstructor.newInstance(str);Context
StackExchange Code Review Q#18826, answer score: 2
Revisions (0)
No revisions yet.