• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Does a synchronized method lock on the whole object or just on his own method?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you attempt to compile and run the following code?




1) Compilation but no output as the run method of slave is not correct
2) Compilation and output of 100 several times followed by 200 several times
3) Compilation and repeated output of 100
4) Compile time error, while cannot be given an unconditional boolean value


I think the right answer is 3. And the reason behind this is that a synchronized method uses the whole class of which the synchronized method belongs too to lock. Hence if one thread enters the synchronized run, it gets the lock on the whole Slave object, and another thread cannot execute the other synchronized method setPrice.

Or would a synchronized method mean one lock per method?? Then if one thread enters the synchronized run method, another thread could enter any other method of the Slave object as long as it does not enter the 'at the moment being used' synchronized run method.

[ August 05, 2007: Message edited by: Marc Wentink ]
[ August 05, 2007: Message edited by: Marc Wentink ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you compiled and ran it?

Also, please QuoteYourSources.
[ August 05, 2007: Message edited by: Ulf Dittmer ]
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf, I know the answer is 3, and you know that I know that. I just want someone to check if my reasoning to get to the right answer is correct.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does a synchronized method lock on the whole object or just on his own method?



I am pretty sure that you know what you meant... but I get nitpicky about these things.

Synchronization is always about using an object to lock on. Java doesn't have a default mechanism that locks a method. The issue is which object is being used to lock on. If the two methods use the same object, then they will block on each other.

If the method is static, then the object used to lock is the Class object for the class. If the method is not static, then the object used to lock is the "this" object.

Henry
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I've also seen, reread, suddenly remembered that

public synchronized void doStuff() {}

is equivalent to

public void doStuff() {synchronized(this)}

on page 709. So it's 100% clear to me now.

By the way, if it makes it more clear what my present scores are, it's about 70% on some free mock exams on Java 1.4. I am going to do Java 5, not 1.4, on friday. Should I post that in a sig or something as Ulf suggests? Please excuse me if I ask something that is somewhere in the book too, I am under a bit of stress and time pressure. I am very pleased with the help I am getting, and trying to give some help too.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic