I have a scheduled method call which on the scheduled time calls the following method:
private void doSomething(Map<String, String> someArguments) throws CustomException {
MyEnum runType = getRunType(someArguments);
switch (runType) {
case FRUIT:
new FruitClass().workNow();
break;
case VEGETABLE:
new VegetableClass().workNow();
break;
default:
// log that the type is not known
}
}
The method signature of workNow is like:
workNow() throws CustomException
workNow method runs for several minutes and does some work. My issue is, when one workNow for FRUIT (or VEGETABLE) is going on and another invoke happens with same type (FRUIT for example), it creates a new FruitClass instance and starts executing its workNow parallelly.
How do I control this behavior? I want the second invocation through second object to wait until first workNow through first object is not complete.
To clarify:
Parallel invocation of
FRUITandVEGETABLEis fine. I want to control parallel invocation of same type. Two or moreFRUITor two or moreVEGETABLE.I can not make
FruitClassandVegetableClassas singletons. I need some wrapping code aroundnewto work how I want.