Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

holdsLock() method in Thread class

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that above method will only return true if it is called from within a synchronized method/code block. But one of mock exam asserts otherwise....
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JavaDocs for java.lang.Thread, holdsLock( Object ) is a static method in the Thread class - it returns false if the executing Thread does not hold a lock on the object specified, true if it does. Locks that the current Thread may have on the object where the method is executed are irrelevant.
Bill
[ August 13, 2003: Message edited by: William Brogden ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill:
In that case the code below should printout "false" because amethod() is not synchronized... Right?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the code below should printout "false" because amethod() is not synchronized... Right?
Have you tested the code to see?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did. I figured the problem. It was not related to lock issue. It was just that assert was not enabled at run time while I presumed it was....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic