aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread Chapter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread Chapter" Watch "Doubt in Thread Chapter" New topic
Author

Doubt in Thread Chapter

Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Hi Ranchers,

Find below the question

A. The notifyAll() method must be called from a synchronized context.
B. To call wait(), an object must own the lock on the thread.
C. The notify() method is defined in class java.lang.Thread.
D. When a thread is waiting as a result of wait(), it release its lock.
E. The notify() method causes a thread to immediately release its lock.
F. The difference between notify() and notifyAll() is that notifyAll() notifies all waiting threads, regardless of the object they're waiting on.

and the answers for the above are A and D. I understand the first answer is correct. but how the option D is correct.

According to my knowledge Thread can oly release its locks by exiting the synchronized code.
I am confused. Let me know what is correct.

Thanks
ANVI
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Hi Anvi,

Taken from Javadoc 1.4

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.


Hope this helps,


Rahul Bhattacharjee
LinkedIn - Blog
Nitin Bhardwaj
Ranch Hand

Joined: Feb 02, 2007
Posts: 72
Hi Anvi,

The Thread also releases its lock on the object when it invokes wait() on that object. However when it gets notified on this object then again it can acquire the lock on this object.
Read each line on Pg 721, and following is a small part of that explanation:

synchronized(anotherObject) { // this has the lock on anotherObject
try {
anotherObject.wait();
// the thread releases the lock and waits
// To continue, the thread needs the lock,
// so it may be blocked until it gets it.
} catch(InterruptedException e){}
}

Nitin
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Thanks Nitin and Rahul for your quick response. But i am still confuse with the logic of release and notify.

Let say we have a Thread A waiting on a particular object say B.Below code will come under the synchronized code:


At this point we have few things.
  • Thread A release the lock on object B.
  • On exit of the synchronized code Thread A will release the lock on object B.[which is not possible here since here the status of thread is block.]
  • Lock is released but still the waiting Thread A will not return to runnable state, unless B is notified.

  • Now here I think if the thread release the lock on object B tha means any other thread can acquire the lock on object B. Rigth?

    Please let me know the points i have wrote down is correct or not?
    thanks
    Anvi
    Rahul Bhattacharjee
    Ranch Hand

    Joined: Nov 29, 2005
    Posts: 2308
    Originally posted by Anvi Dixit:

    Now here I think if the thread release the lock on object B tha means any other thread can acquire the lock on object B. Rigth?


    Yes , once the monitor is released , it can be acquired by other threads and this thread goes to the wait set for the monitor.
    [ July 30, 2007: Message edited by: Rahul Bhattacharjee ]
    Nitin Bhardwaj
    Ranch Hand

    Joined: Feb 02, 2007
    Posts: 72
    Hi Anvi,

    Let's try to understand with the help of an example given in Kathy's book:
    1. class ThreadA {
    2. public static void main(String [] args) {
    3. ThreadB b = new ThreadB();
    4. b.start();
    5.
    6. synchronized(b) {
    7. try {
    8. System.out.println("Waiting for b to complete...");
    9. b.wait();
    10. } catch (InterruptedException e) {}
    11. System.out.println("Total is: " + b.total);
    12. }
    13. }
    14. }
    15.
    16. class ThreadB extends Thread {
    17. int total;
    18.
    19. public void run() {
    20. synchronized(this) {
    21. for(int i=0;i<100;i++) {
    22. total += i;
    23. }
    24. notify();
    25. }
    26. }
    27. }

    Explanation: When the main thread (class ThreadA) reaches line number 9 then it releases lock on Object 'b' which itself is a ThreadB object and now it becomes available for other Threads. So at some point of time thread 'b' which was started on Line 4 will be in running state means will execute it's run() method.
    Now think if on Line no 9 the main thread would not have released the lock on ThreadB object 'b' then would it be able to acquire the lock at line number 20? No. This is the whole point of releasing a lock when a thread waits on an object.
    So now ThreadB object b executes its run() method by acquiring a lock on itself and calculating the sum of numbers. After this it calls notify() at line number 24. This tells the thread(currently main thread) which is waiting for a notification on this object that lock has been released and the main thread again comes to runnable state and when it again starts running it will again acquire lock on object 'b'.
    At the top level view you can think like main thread have to wait for the total to be calculated by the ThreadB thread 'b' before it can print b.total . So we achieve this thing by wait() and notify() method.

    Huh !! Now I need rest
    Enjoy !!
    Nitin
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt in Thread Chapter
     
    Similar Threads
    sample question on threads
    Doubt in Thread Chapter
    synchronized, wait, notify
    Using wait and notify with threads
    synchronized block?