Avoid the condition. Make your expression as stateless as possible. Each thread (sequence of actions) must take a context at the beginning and use this context, passing it from method to method as parameters.
When this method does not solve all your problems, use an event driven mechanism. When your code needs to share something with other components, it sends an event (message) to some bus (topic, queue, whatever). Components can register listeners to listen for events and respond appropriately. In this case, there are no race conditions (except for inserting events into the queue). If you use a ready-to-use queue, rather than its own encoding, it should be quite efficient.
Also take a look at the Actors model.
Alexr source share