Does this requires a volatile or is the thread guaranteed to see the latest value?
No, volatile
is not required. That Java language definition says that the thread that is created will see all updates made by the creating thread before start()
is called.
See Synchronization Order (JLS 17.4.4). To quote:
- An action that starts a thread synchronizes-with the first action in the thread it starts.
Obviously, if the value is updated after the start()
or if the new thread makes changes to the value that the starting thread needs to see then memory synchronization is needed.
The same holds true if you call thread.join()
. Any updates made by thread that was joined with are seen by the caller after the join()
returns.