My ansver to flowchart above:
steps:
1, 2 - correct,
3.0 - this before 3 - JVM lock instance for changing and reading
3 - not, first alocated memory for reference value
4 - not, set alocated memmory to value of pointer to allocated space to
Object (new Object())
4.1 - after this JVM unlock read access to instance
5 - calling constructor
6 - full unlock instance

So, if you lock at steps:
If thread 2 detect of one of condition instance != null (this may only
after step 4.1), then return it. If this not initialise - is not
harmfull traoubles, becose, to changing this object is lock by JVM, and
this be unlocked only full initialisation.

PS. Guys, thank you for discussion, but let's move to private emailing
about this, please.

-----Original Message-----
From: Jonas Kvarnstr�m [mailto:[EMAIL PROTECTED]] 
Sent: Friday, March 22, 2002 11:04 AM
To: IDEA Early Access Program
Subject: RE: [Eap-list] BUG: Code Inspection: Singleton not recognized


On Fri, 2002-03-22 at 02:18, Tom Wagner wrote:
> >If I remember correctly, this is what could happen:
> >
> >(1) Thread 1 sees that instance==null.
> >(2) Thread 1 synchronizes on the synchronizer.
> >(3) Thread 1 allocates X bytes of memory for a FileCache.
> >(4) Thread 1 sets instance=(pointer to the allocated memory).
> >(5) Thread 1 calls FileCache() constructor to initialize
> >    the memory area that instance points to -- AFTER it has
> >    already set the instance pointer!
> >(6) Thread 1 unlocks the synchronizer.
> >
> 
> The problem is that you could then have another thread that runs after

> step 4.  It sees that instance is not null and attempts to use it. 
> Ooops, it hasn't actually been constructed yet, so the program blows 
> up.

Yes, that's exactly what I wrote below that example.  I skipped the part
about multi-processor systems, though (that even if thread 1 sets the
instance pointer after calling the FileCache() constructor, thread 2
could see the instance pointer being set before the FileCache
initialization due to the lack of synchronization between the
processors), so thanks for clearing that up.




_______________________________________________
Eap-list mailing list
[EMAIL PROTECTED] http://www.intellij.com/mailman/listinfo/eap-list

_______________________________________________
Eap-list mailing list
[EMAIL PROTECTED]
http://www.intellij.com/mailman/listinfo/eap-list

Reply via email to