• 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

Parent Thread gets the same object lock has child threads? Confused!!

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

I have the following code:


When I run this i get the following output


On lines 4 and 5 of the output the jvm is telling me that both the main thread and Thread 8 have the lock over the hash hashtable object?
How can two different threads have the lock on a same syncronized object??? I'm lost here.... Is it because thread 8 (and 9) were started by the Main thread (are child threads of main thread)?

Regards
 
Ranch Hand
Posts: 32
Scala IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method Thread.holdsLock(Object) is static. That means it is a method associated with the class Thread that returns whether the current Thread is holding the lock. Unfortunately, java allows accessing static members via concrete instances (this is, however, discouraged by most IDEs and best practice guides). 'mainT.holdsLock(hash)' effectively resolves to 'Thread.holdsLock(hash)' - admittedly this is rather ugly and intuitive.
 
V Pinto
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I totally missed that holdsLock() is a static method, so basically holdLock always refers to current Thread executing the add method and not to the main Thread as I was aiming for.
Thanks for the reply!
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic