• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

YES (Yet Another Synchronization) question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic