Michael Farragher wrote:2). Does it matter about the thread safety of the type you're using ? I've read that any type will do.
Campbell Ritchie wrote:Can you prevent that latter problem by synchronising the constructor, Stephan?
I know you can sometimes cause that particular problem by allowing a this reference to escape the constructor.
Michael Farragher wrote:lock must be final. Otherwise, another thread could read it as null
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.
Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.