Variable in Parallel Multi Instance

Assume we have a parallel multi instance script task that does somethig like:

  • do stuff
  • read variable x
  • modify x
  • update x

So x could be a counter, then we would get x = 5, do x = x + 7 (e.g. number of changes done) and save x.

Is there any way to make the code from reading to updating of the variable atomic? Alternatively if we could lock and release the variable this would work too (similar to fetch and lock for external tasks). But I haven’t found any information about this.

Why is it important? Because the task is executed in prallel it is possible that two task instances read the variable before the other task updated it and then the update of one of the tasks is getting lost.

I still have the same question after a year.

For this specific problem the best thing I can think of would be to create a singleton (bean) with a synchronized method addToVariable(variableName, amount) and all variable updates would have to happen through this method. We would have to create a custom end point for external tasks. But this somehow doesn’t feel right, not generalized enough and tedious.

Are there better solutions?

GetAndLock and UpdateAndRelease for variables or similar mechanics would be really helpful for highly parallelized processes.