# How does workflow thread synchronization work?

**URL:** <https://community.temporal.io/t/how-does-workflow-thread-synchronization-work/504>\
**Category:** Community Support\
**Tags:** java-sdk, signals\
**Created:** [August 21, 2020, 5:43pm UTC](https://community.temporal.io/t/how-does-workflow-thread-synchronization-work/504 "2020-08-21T17:43:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [August 21, 2020, 5:55pm UTC](https://community.temporal.io/t/how-does-workflow-thread-synchronization-work/504/2 "2020-08-21T17:55:23Z")

</div>

> How does Temporal Java SDK guarantee that jobQueue did not change between `this.jobQueue.size() > 0` and `Workflow.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](https://github.com/temporalio/java-sdk/issues/28) to add a deadlock detector to make troubleshooting such programming mistakes easier.

---

_[View the full topic](https://community.temporal.io/t/how-does-workflow-thread-synchronization-work/504)._
