• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Doubt in Thread Ques

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

I have found the following ques in one of the mock exams.
Correct answer to this is A & D. I'm convinced about D but I have doubts about A. Can anyone clarify this option with example. Thanks.

Which of the following statements are true?

Select all valid answers.

A. A thread can reacquire the lock on the same object when it already owns the lock.

B. The performance of synchronized methods is better than non-synchronized methods.

C. The notify() method is called on the thread executing the code.

D. A thread can have locks on different objects at the same given time.

E. The notifyAll() method is called on the threads waiting for the lock of an object or a class.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sample extends Thread{

public void run() {
synchronized(this) {
System.out.println("Print me");
synchronized(this) {
System.out.println("Print me too");
}
}
}

public static void main(String args[]){
Thread t1 = new Sample();
t1.start();
}
}
 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Balaji for the example. It very well supports the option A.
But for all practical purposes, I don't think we'll ever need to reacquire the lock on the same object if we already have one. This may be 'legal' in Java but may not be 'appropriate'.
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very True
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if you have 2 synchronized methods in your class and the first one calling the second one ?

Then definitely reaquiring of lock is necessary if java doesn't support this kind of reentrant locking.
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, the locking is at Object level. Ideally, how a thread handles another sync block for the same object that it already holds a lock is hidden.
 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess we can support the above post with the following example (its the modified version of Balaji's example).

 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just found a text (Chapter 9, page 523) in K&B book which clearly states that a thread is free to call other synchronized methods on the same object, using the lock the thread already has. That means a thread is not actually 'reacquiring' the lock, rather it is 'reusing' the same lock.
 
Greenhorn
Posts: 6
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the reason behind the term 'reenterant' the same phenomenon . however the lock hold count is inceremented each time the same lock is acquied .
hope i'm not wrong
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic