Like I said making the builder a static inner class or a top-level class makes no difference regarding concurrency.
Each thread creates its own Builder object that is not shared between threads. Each thread fills it individually and calls build(). Afterwards you have a perfectly thread safe immutable object that you can make available to multiple thread.
The Builder object itself is not thread safe. But it doesn't need to be, since it should not be shared between threads. Sharing the Builder object between threads circumvents the whole point of this pattern and I would say its quite bad design.
So this is thread safe unless you use it in a unsafe way.
I agree, the internals of the class (i.e. non final fields) would determine if it's thread safe or not.
I agree, in the case where the builder is static is not thread safe. Why shouldn't it be shared between threads though? My understanding of this pattern is that it's about object creation, in a way that makes the code more maintainable and reduces the chances of objects with incorrect state. I haven't seen any mention of concurrecny.
I've put 3 possible builder implementations in my pattern-play repo, as you may have guessed I use it for learning patterns github.com/ethomev/pattern-play/tr...
I like the pattern where there is only one constructor for the object and it takes the builder. I think I will use this implementation the next time I need to use the builder pattern.
P.S. Sorry for the delay in replying, I'm now a father of 2 so I have less free time ;-)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Like I said making the builder a static inner class or a top-level class makes no difference regarding concurrency.
Each thread creates its own Builder object that is not shared between threads. Each thread fills it individually and calls build(). Afterwards you have a perfectly thread safe immutable object that you can make available to multiple thread.
The Builder object itself is not thread safe. But it doesn't need to be, since it should not be shared between threads. Sharing the Builder object between threads circumvents the whole point of this pattern and I would say its quite bad design.
So this is thread safe unless you use it in a unsafe way.
I agree, the internals of the class (i.e. non final fields) would determine if it's thread safe or not.
I agree, in the case where the builder is static is not thread safe. Why shouldn't it be shared between threads though? My understanding of this pattern is that it's about object creation, in a way that makes the code more maintainable and reduces the chances of objects with incorrect state. I haven't seen any mention of concurrecny.
I've put 3 possible builder implementations in my pattern-play repo, as you may have guessed I use it for learning patterns
github.com/ethomev/pattern-play/tr...
I like the pattern where there is only one constructor for the object and it takes the builder. I think I will use this implementation the next time I need to use the builder pattern.
P.S. Sorry for the delay in replying, I'm now a father of 2 so I have less free time ;-)