File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread Ques Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread Ques" Watch "Doubt in Thread Ques" New topic
Author

Doubt in Thread Ques

Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
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.


SCJP 1.4 - 95%
Balaji VR
Ranch Hand

Joined: Mar 22, 2006
Posts: 76
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();
}
}


Bala<br />SCJP 1.4 98%<br />SCBCD 1.3 -- 88%
Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
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

Joined: Mar 22, 2006
Posts: 76
Very True
Srinu Nanduri
Ranch Hand

Joined: Mar 20, 2006
Posts: 42
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.


SCJP, SCWCD
Balaji VR
Ranch Hand

Joined: Mar 22, 2006
Posts: 76
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

Joined: May 09, 2006
Posts: 57
I guess we can support the above post with the following example (its the modified version of Balaji's example).

Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
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.
sherin stephen
Greenhorn

Joined: May 14, 2006
Posts: 6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Thread Ques