Reputation: 4332
For example -
public class Request {
public String id; //is it visible to other threads after construction?
public Request(String id){
this.id= id;
}
}
Upvotes: 3
Views: 442
Reputation: 328785
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:
final
volatile
Request
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.
Upvotes: 3