How does Temporal Java SDK guarantee that jobQueue did not change between
this.jobQueue.size() > 0
andWorkflow.continueAsNew(this.jobQueue);
lines? DeterministicRunner documentation indicates that SignalMethod will be given a chance to run before workflow completes. And that would indicate that last signal could be lost as workflow would decide to exit.
Temporal uses cooperative multithreading to provide this guarantee as well as ensure deterministic execution of multithreaded code. Only one thread is executed at a time and switch to another thread can happen only when a blocking operation using Temporal provided primitives is called. Examples of such operations are Workflow.sleep
, Promise.get
, Workflow.await
. As no blocking operations are called between this.jobQueue.size() > 0
and Workflow.continueAsNew(this.jobQueue);
lines, no other thread can intervene.
In general it is possible for thread to be interrupted in between those 2 lines. In a typical java application I would put both if-clause and Workflow.continuesAsNew inside of a single critical section. But it seems to be not recommended here.
It is not recommended as standard Java synchronization can block threads in places that Temporal doesn’t have control over. It leads to deadlocks. Here is an issue to add a deadlock detector to make troubleshooting such programming mistakes easier.