I am trying to write a decorator to an existing class that rejects if there are not enough resources available. Here is an example version of the code with no multithreading at all:
public interface Handler {
boolean handleTask(Task myTask); // returns true if successful, false if failure
}
public interface Task {
int getResources();
// Among other things
}
public BlockingHandler implements Handler {
private int occupied;
private final int limit;
private final Handler backingHandler;
BlockingHandler(Handler backingHandler) {
this.backingHandler = backingHandler;
}
@Override
public boolean handleTask(Task myTask) {
if(myTask.getResources() + occupied > limit) return false;
occupied += myTask.getResources();
return backingHandler.handleTask(myTask);
}
// Don't worry about this part, I'm not doing it this way, I just want the code to make sense
public void notifyResourceRelease(Task finishedTask) {
if(finishedTask.isDone()) occupied -= myTask.getResources();
}
}
The problem is, this handleTask
method can be called on multiple threads, and I want to be very fast (i.e. avoid synchronized
). It's not enough to make occupied
be volatile
or an AtomicInteger
, because a race condition is still possible, for instance:
Thread 1: call handleTask
Thread 1: call atomicOccupied.get()
Thread 2: call handleTask
Thread 1: evaluate if condition
Thread 2: call atomicOccupied.get()
Is it possible to do this without using synchronized
? Is there, for instance, an extended AtomicInteger
class with a more powerful compareAndSet
?