aspose file tools
The moose likes Threads and Synchronization and the fly likes How is this a safe publication? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "How is this a safe publication?" Watch "How is this a safe publication?" New topic
Author

How is this a safe publication?

Gagan Sabharwal
Ranch Hand

Joined: Apr 23, 2006
Posts: 48
Hi,

I am going through the following url
Java theory and practice - IBM

Under the section, What do you mean by "publish"? , the following code is written



1) The respected author says that it is a safe publication. I fail to understand why ? The contention in my mind is that what if the compiler or processor or cache reorders

statement in the constructor and puts it as the first statement in the constructor. Won't the inner class MyThread be able to see a partially constructed object ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3065
    
    1

Yeah, but it's a private inner class right? You can trust it not to abuse the reference.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 376

Well its written by Brian Goetz who is usually right, however its appears to be written 10 years ago and before JSR133 so I guess Brian's advice would be not to read it and look up something more relevant :-)

I think that the example has issues and I suspect if he rewrote it today we'd see final being used as he hints at the end.

The start method looks unsafe for several reasons, he appears to be trying to demo the equivalent of

A call to start() on a thread happens-before any actions in the started thread.
in the new memory model.

... so if a thread constructs an object and then it starts a thread that uses that object , that has happens before ordering (at the point of starting)... fine I can see where he's going) ...

... but there is no guarantee in his example the thread that constructs the object calls the start as well but then if that's allowable you have a much simpler problem(ie just reading a non final field thread with no happens before ordering ) ..... by not having the start in the ctr (very good idea to not have it there off course) he's given himself another problem in that you can't tie the ctr and start to the same thread. It's possible I've missed something but can't see it, a bit confusing at best.


The quick answer I think this article is redundant given its age and if I'm sure would at least use final if written today, which I think he hints at.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Gagan Sabharwal
Ranch Hand

Joined: Apr 23, 2006
Posts: 48
Thanks people.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How is this a safe publication?
 
Similar Threads
do not publish partially initialized objects?
Thread, Runnable and their run() method
Using "this" keyword in parent class thread safe?
Thread Doubt
Threads and a local variable