I am working on a simple component where my class holds a reference to an object that support add operations and a submit function.
Once the submit function is called, the referenced object needs to be replaced with a new one.
My API supports add operations that submits the item once it reached a certin size BUT in some cases i will need to verify that an item has been submitted regardless of its size after some time.
The only solution i could think of is synchronizing the entire class, which i would like to avoid. Any thoughts of other solutions/possible refactoring to avoid synchronizing everything?
pulic class MyClass{
private MyObject ref;
private int maxObjectsToSubmit;
public MyClass(int maxItems){
this.maxObjectsToSubmit = maxItems;
}
public void add(Item obj){
ref.add(obj);
if(ref.size() == maxObjectsToSubmit){
submit();
}else if(obj.isVeryImportant()){
SomeAPI.runThreadInExactDelayToCreateRaceCondition(new Runnable{
public void run(){
//Submit only if o is still waiting to be submittet.
// if ref does not contains obj than it has already been submitted
if(ref.contains(obj)){
submit()
}
})
}
}
private void submit(){
SomeAPI.doSomething(ref);
this.ref = new MyObject();
}
}
Race condition is:
- T1 adds some items untill a veryImpotant item arrives, which starts T2
- T1 reaches maxObjectsToSubmit and ref.size() == maxObjectsToSubmit returns true
- T2 starts, ref.contains(obj) returns true
This can result in an empty object being submitted / Same object being submitted twice/etc'