Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread Chapter

 
Anvi Dixit
Ranch Hand
Posts: 45
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2308
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 72
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Nirmal Seneviratne
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello guys, I have a doubt about the statement E
    E. The notify() method causes a thread to immediately release its lock.

    Are these statements correct? (I doubt E is incorrect because of these)
    "A thread can only release its lock by exiting the synchronized code or calling wait()"
    "notify() just makes the thread go to runnable state from wait state"
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21510
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    First, welcome to the ranch.

    Nirmal Seneviratne wrote:Hello guys, I have a doubt about the statement E
    E. The notify() method causes a thread to immediately release its lock.



    Nope., The original post is correct. Choice E is *not* correct, as the notify() method does *not* cause the thread to release the lock.

    Nirmal Seneviratne wrote:
    Are these statements correct? (I doubt E is incorrect because of these)
    "A thread can only release its lock by exiting the synchronized code or calling wait()"
    "notify() just makes the thread go to runnable state from wait state"


    Both of these statements are correct ... and neither of these statements has an effect on the first part of this question. Can you tell us why this puts doubts on choice E being incorrect?

    Henry
     
    Nirmal Seneviratne
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Henry!!
    and neither of these statements has an effect on the first part of this question.

    did you mean that even if those two statements are right, that does not cause E statement not to be correct?
    E. The notify() method causes a thread to immediately release its lock.


    Simply, I think if those two statements are correct, then the E statement is obviously not correct !! right ?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic