For example -
public class Request {
public String id; //is it visible to other threads after construction?
public Request(String id){
this.id= id;
}
}
As it is your class is not thread safe and a thread could observe a null value for id even after the constructor has finished.
To make sure id is visible to all threads after construction, you have several possibilities:
finalvolatileRequest object.Safe publication idioms include:
See also this other post which explains the importance of marking fields final to guarantee the thread safety of immutable objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With