Execution set variable for concurrent tasks (tasks in a parallel block)

Hello guys,

I have a problem using set variable in a delegation expression java class.
I have the following diagram, which has a parallel gateway followed by one task which is async before but not exclusive.
When the token reaches the first parallel task in the upper branch it should call a delegation class in which i perform a while loop. The thread stays as long in the while loop as one variable will change. A second thread goes in the other branch and calls the same method where it stays in the while loop as long as a specific variable has been changed. The weird thing here is that in both branches i update the variable but the other thread cannot see the updated value and stays forever in the while loop.

How can i overcome this behaviour?

The idea is that i have a variable on the gateway and this variable is “true”. What i want is to set it “false” but only if both parallel tasks have already started. Both tasks starts but the hang in the while because the cannot see the update from the other thread.

Here is my example diagram:
P1-Model.bpmn (14.3 KB)

Here is my java code:

while(execution.getVariable(“token_”+valueBID).equals(“0”)) {
sequenceTokenValueA = (String) execution.getVariable(“token_”+flowIdOwnPath.getId());
flows = flowIdOwnPath.getSource().getOutgoing();

  for(SequenceFlow tmpFlow : flows) {							
       if(!flowIdOwnPath.getId().equals(tmpFlow.getId())) {
  	         sequenceTokenValueB = (String) execution.getVariable("token_"+tmpFlow.getId());
  		 valueBID = tmpFlow.getId();
  		}
   }					
   if(execution.getVariable("token_"+flowIdOwnPath.getId()).equals("1") && >execution.getVariable("token_"+valueBID).equals("1")) {
  		execution.setVariable("token_"+flowIdOwnPath.getId(), "1");
  		execution.setVariable("token_"+ valueBID, "1");
  								
  		execution.setVariable("token_"+prevFlowNode.getId(), "false");
  		execution.setVariable("token_"+execution.getCurrentActivityId(), "true");
  		break;
  	} else {
  		Thread.sleep(2000);
  								
  	if(execution.getVariable("token_"+flowIdOwnPath.getId()).equals("0")) {
  		execution.setVariable("token_"+flowIdOwnPath.getId(), "1");
  								
  		RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
  		runtimeService.setVariable(execution.getId(), "token_"+flowIdOwnPath.getId(), "1");
  	}
  }
}

Here is my ouput as i logged everything:

As you can see the values for valueA Flow_12ii9xh and also the other one valueA Gateway_1r8pyy3_task8 will be set to 1.

If i open camunda engine i cannot see the updated values. It is still the default value of 0.

Best Regards Sebastian.

Of course i know the fact that the transactions are not committed and thats why i cannot access. But is there any other way to do this? Or is there any command to flush the current state or something like this?