Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Are Class lock and Object lock independent of each other ?

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I haven't been able to get a concluding line anywhere on this matter...

After reading Heller & Roberts and Khalid Mughal, I safely assumed that when a thread acquires the lock on Class object ( by executing a synchronized static method ), it keeps any other thread trying to execute any other synchronized static method in the same class, waiting. However, other threads can acquire a lock on an object of the above mentioned class and execute a synchronized instance method or a normal method. This means, that the class level lock and the object level locks are independent of each other or in other words, synchronization of static methods in a class is independent from the synchronization of instance methods on objects of the class.

Then I came across these old discussions, E.g.

here

( some of the members say that once a class level lock is obtained, then no other thread can access ANY METHOD on ANY OTHER OBJECT including the object used by the current thread, UNTIL this thread releases this Class lock. )

here

and here


Why this mixed opinion ? What is correct now ?! Can anyone please throw some more light ?

thanks,
Soumya
[ June 01, 2005: Message edited by: soumya ravindranath ]
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just adding a bit of code to the post above.


Output -

Thread[StatThread,5,main] : i = 0
Thread[SynThread,5,main] : k = 0
Thread[PlainThread,5,main] : j = 0
Thread[PlainThread,5,main] : j = 1
Thread[PlainThread,5,main] : j = 2
Thread[PlainThread,5,main] : j = 3
Thread[PlainThread,5,main] : j = 4
Thread[StatThread,5,main] : i = 1
Thread[SynThread,5,main] : k = 1
Thread[StatThread,5,main] : i = 2
Thread[SynThread,5,main] : k = 2
Thread[StatThread,5,main] : i = 3
Thread[SynThread,5,main] : k = 3
Thread[StatThread,5,main] : i = 4
Thread[SynThread,5,main] : k = 4


this shows that two threads can simultaneously call a sync. class method and a sync. instance method.
My question remains as to why some people firmly believe that it is otherwise. There must be some reason... ?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


After reading Heller & Roberts and Khalid Mughal, I safely assumed that when a thread acquires the lock on Class object ( by executing a synchronized static method ), it keeps any other thread trying to execute any other synchronized static method in the same class, waiting. However, other threads can acquire a lock on an object of the above mentioned class and execute a synchronized instance method or a normal method. This means, that the class level lock and the object level locks are independent of each other or in other words, synchronization of static methods in a class is independent from the synchronization of instance methods on objects of the class.


This is correct.
I didn't read your linked references in any detail, however, I have come across similar claims in the past. This is often due to the author of the claim to overlook the different contexts in which a method can exist (for example, static and instance methods and what about, for example, abstract methods? (they are, after all, methods))?
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it right. As you said, the class level lock and the object level locks are independent of each other. I have no idea why some people belive otherwise!
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my God! your name is on my book !
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that means you should give it back to him - he's probably been looking for it. I put my name in all my books in case something like this happens.
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have all reasons to believe that it's his autograph on my book
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic