Marc, I don't entirely understand what you have pointed out here. It sounds important,
but I have to admit I don't get it.
Do you mean to say that, for proper synchronization, we must synchronize the whole
getInstance method? I would think that if
another thread tried to access any of the class's fields, it would either be blocked
at the synchronized block (if it was in
process of obtaining a reference for the first time), or it would already have a valid
reference to the singleton, in which
case why would the fields be bogus?
And how can a thread pre-read contents of an object, if it has yet to obtain a bona
fide instance of it?
I am still finding my thread sea legs, so to speak, so apologies for being so blind to
this.
-Armen
> On Wed, 13 Oct 1999, Peter Pilgrim wrote:
>
> > Or perhaps alternatively the lazy singleton.
> >
> > public class NetworkPrinter {
> >
> > protected PaperStack paper
> > protected static Object locker = new Object();
> > private static NetworkPrinter thePrinter = null;
> >
> >
> > private NetworkPrinter ( )
> > {
> > }
> >
> > public static NetworkPrinter getInstance()
> > {
> > if ( thePrinter != null ) {
> > // Thread Safety - Double Guard technique
> > synchronized( locker )
> > if ( thePrinter != null ) {
> > thePrinter = new NetworkPrinter();
> > }
> > }
> > return (thePrinter);
>
> (yea, this is somewhat offtopic, but...)
>
> This is not properly synchronized, as I have been taught by someone with
> more sense than I have.
>
> The basic problem revolves around the fact that synchronization is
> not just for serialization but is also for visibility. ie. without
> synchronization, you are not always guaranteed that a change made by
> one thread is visible in another. Sometimes this is ok.
>
> Although the JLS guarantees that the thePrinter reference must be
> fully visible if it is visible, it doesn't guarantee that instance
> variables, etc. of the class that thePrinter references are also
> visible. So if the creating thread is still in the synchronized
> block, then the value stored in thePrinter may be copied to main
> memory, but things like instance variables may not be, so when
> another thread tries to use them they are bogus.
>
> In addition, apparently another thread could have pre-read the contents
> of the object referenced by thePrinter before it reads the reference
> in thePrinter.
>
> What it boils down to is you can't cheat on synchronization.
>
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]