File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

Beaker
Greenhorn

Joined: Jul 07, 2005
Posts: 1
If I have a class similar to the one below, and have a synchronized block for the object level lock on "_object", will that prevent the getStaticObject() method from returning the "_object", which may be null, if the reinitObject() method is called?


It seems like both methods should be static and synchronized, and the synchronized block should synch on the class level lock... to prevent the getStaticObject() method from returning a null, right?


Which is the best approach to preventing getStaticObject() from ever returning null?

Thanks,
Becker
[ July 07, 2005: Message edited by: Beaker ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"Beaker A. Muppet": Welcome to JavaRanch! Please take a look at our display name policy and edit your display name to something less obviously farcical. Thank you.

Also, most of us find it difficult to read posts in which the code has no formatting. Please use [code] tags to get the formatting to display so it's readable. You can edit your previous post using the little pencil-and-paper icon at the top of the post.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

My understanding is...

To safeguard a static variable, you need to synchronize on the class rather than a particular object. Otherwise, different threads could obtain object locks of different instances and access the (same) static member in an "unsynchronized" manner.

Because your methods are already static, you only need the "synchronized" keyword before the return type -- you do not need a separate block synchronized explicitly on the class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads and Synchronization...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

There are things I could say, but you would learn enough already buy creating code that compiles first. Start there.
 
Don't get me started about those stupid light bulbs.
 
subject: synchronized block
 
Similar Threads
Java Singleton Pattern Tutorial
Clarification in wait, notify and notifyAll methods
q from jiris.com
synchronized(ObjectInstance){ //satements } AND Polymorphism
Need help on JTree