wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes YES (Yet Another Synchronization) question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "YES (Yet Another Synchronization) question" Watch "YES (Yet Another Synchronization) question" New topic
Author

YES (Yet Another Synchronization) question

Bala Mohan
Greenhorn

Joined: Jul 05, 2002
Posts: 27
Hi all,
Say, I have N threads competing access and modify an object.
All I have to do is qualify all the methods of that object
with 'Synchronized' keyword. This would automatically force
'synchronous' access to that object. None of the accessing threads
need to wait/notify. Am I correct?
Thanks in advance.
Bala.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If all of the methods of an object are synchronized, and all of the data members of that class are private, and the class doesn't make any assumptions about the order in which its methods should be called, then yes, you can be reasonably confident that the object can be used in a multithreaded environment safely (of course, you can get deadlocks -- but you won't get corrupted data.) The various threads can act as if no other threads exist, and don't need to use wait() or notify() for anything.


[Jess in Action][AskingGoodQuestions]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Bala Mohan:

All I have to do is qualify all the methods of that object
with 'Synchronized' keyword. This would automatically force
'synchronous' access to that object.

read retraction below!
Not quite. Using the 'synchronized' keyword on a method only controls multiple threads' access to that method. It does not prevent other threads from accessing other methods in the same object, even if those other methods are synchronized. You have to use synchronized blocks within methods that block on 'this' to insure only one thread is altering an object's state at a particular time.
read retraction below!
[ February 20, 2004: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Using the 'synchronized' keyword on a method only controls multiple threads' access to that method.
I don't believe this is correct. No two synchronized nonstatic methods may execute simultaneously if invoked from the same object. Similarly, no two synchronized static methods may execute simultaneously, if they're from the same class. Confusingly though, two synchronized methods from the same class/instance may exectue simultaneously if one is static and the other is not. That's because

is equivalent to

while

is equivalent to

Static and nonstatic methods sync on two different things, so interactions between them can be complex. Typically it's best to avoid this possibility entirely - if you have synchronized nonstatic methods in a class, don't allow any synchronized static. Or vice versa.


"I'm not back." - Bill Harding, Twister
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Originally posted by Jim Yingst:
I don't believe this is correct. No two synchronized nonstatic methods may execute simultaneously if invoked from the same object.

You are correct. I went back to the code I thought was exhibiting such behavior and it turns out that multiple threads are able to invoke synchronized methods simultaneously because invoking wait() releases the lock on the object. A simplified example would be a Queue class where get() and put() are synchronized and get() blocks with wait() if there are no objects in the queue. I had gotten to thinking this proved my previous point, but as I said, when the consumer thread blocks on wait() he releases the synchronized lock, allowing the producer to enter the put() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: YES (Yet Another Synchronization) question
 
Similar Threads
Calling explicitly destroy method in service method of servlet?
Help me on Threads!!!
About Garbage Collection
Multiple threads of a servlet instance and servletConfig
How do I pass a update a variable to all running Threads?