Home arrow static arrow Java Programming [Archive] - Two questions about thread and lock.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Two questions about thread and lock.
This topic has 11 replies on 1 page.

Posts:536
Registered: 6/29/03
Two questions about thread and lock.  
Jul 6, 2004 12:13 AM



 
Hello, everyone:

I have two basic questions to verify which are dealing with thread and lock. The first question is: the lock of a synchronized block of code will be automatically released when a thread leaves the code block. For example,

synchronized (lock){    //block of synchronized block;} //some works after executing synchronized block


I think when a thread leaves the synchronized block and doing "some works after executing synchronized block", the lock of the synchronized code block will be released automatically. Am I correct?

My second question is, when notify method of an object is called, the lock of object is not automatically released, even though wating threads will be awaken to check waiting condition again. For example, when a thread execute the following statement,

object.notify();


the "lock" of "object" is not released by current thread. Am I correct?

Thanks in advance,
George

 

Posts:11,200
Registered: 7/22/99
Re: Two questions about thread and lock.  
Jul 6, 2004 12:27 AM (reply 1 of 11)



 
the lock of the synchronized code block will be released automatically.
Am I correct?

Yes, that's correct. You don't need to anything more for the thread to release the lock.

the "lock" of "object" is not released by current thread. Am I correct?

Yes, the lock is not released until the current thread exits its synchronized block. And the notified thread needs to lock the same object to continue its work so it'll wait until the current thread releases it.
 

Posts:536
Registered: 6/29/03
Re: Two questions about thread and lock.  
Jul 6, 2004 3:31 AM (reply 2 of 11)



 
Thanks, jsalonen buddy!

I have found a sample from java.sun.com,

public synchronized int get() {    while (available == false) {        try {            // wait for Producer to put value            wait();        } catch (InterruptedException e) {        }    }    available = false;    // notify Producer that value has been retrieved    notifyAll();    return contents;}public synchronized void put(int value) {    while (available == true) {        try {            // wait for Consumer to get value            wait();        } catch (InterruptedException e) {        }    }    contents = value;    available = true;    // notify Consumer that value has been set    notifyAll();} 


I think in function "get()", lock will not be released until "return content" is invoked (not immediately released after "invoke" function is invoked). Am I correct?

Another idea, I think in a synchronized block, the lock of another object can be invoked, for example,

synchronized (lock1){    lock2.notify();}


I think in this case, threads waiting for the lock of "lock2" will be notified BUT it is NOT sure that "lock2" is released, since "notify" statement does not release the lock at all. Am I correct?

Best regards,
George

 

Posts:2,909
Registered: 13.8.2003
Re: Two questions about thread and lock.  
Jul 6, 2004 3:36 AM (reply 3 of 11)



 
I think in function "get()", lock will not be released
until "return content" is invoked (not immediately
released after "invoke" function is invoked). Am I
correct?

Yes, the lock is released when the get() method exits.

Another idea, I think in a synchronized block, the
lock of another object can be invoked, for example,

synchronized (lock1){lock2.notify();}

That is false, you will receive an exception at runtime saying that the thread doesn't own the monitor for that object. You can only wait/notify on objects you have synchronized on.
 

Posts:536
Registered: 6/29/03
Re: Two questions about thread and lock.  
Jul 6, 2004 3:54 AM (reply 4 of 11)



 
Thanks, Kayaman buddy!

It makes me puzzled that what is the real function of "notify()" method. Since if we invoke "notify()", the waiting threads will not be real awakened and continue to execute from waiting position until we leave the synchronized block, i.e. the time when the lock will be real released. But if we does not invoke "notify()" at all, the effect is the same, i.e. waiting threads only have chance to execute after we leave synchronized block. So, what is the function of invoking "notify()" inside a synchronized block? Is it useless?

Best regards,
George

 

Posts:6,147
Registered: 11/9/00
Re: Two questions about thread and lock.  
Jul 6, 2004 4:00 AM (reply 5 of 11)



 
Threads can be in 4 states with respect to a particular monitor.

1) Not involved

2) Trying to own (and therefore waiting)

3) Owning

4) in wait()

What nofify does is move a thread from 4) to 2)

If there are several threads in state 4) which is moved is undefined.

A thread which is in wait() won't resume just because the monitor is cleared (i.e. a thread releases it and no new thread claims it).
 

Posts:536
Registered: 6/29/03
Re: Two questions about thread and lock.  
Jul 6, 2004 4:12 AM (reply 6 of 11)



 
Thanks, malcolmmc buddy!

Do you mean a waiting thread will not be resumed to execute until the "notify()" method of the monitor is invoked? If that so, I think leaving a synchronized block, i.e. release the lock of monitor, can not awake waiting thread directly until "notify()" of monitor is invoked?

Best regards,
George

 

Posts:2,909
Registered: 13.8.2003
Re: Two questions about thread and lock.  
Jul 6, 2004 4:13 AM (reply 7 of 11)



 
Do you mean a waiting thread will not be resumed to
execute until the "notify()" method of the monitor is
invoked? If that so, I think leaving a synchronized
block, i.e. release the lock of monitor, can not awake
waiting thread directly until "notify()" of monitor is
invoked?

Exactly.
 

Posts:6,147
Registered: 11/9/00
Re: Two questions about thread and lock.  
Jul 6, 2004 5:33 AM (reply 8 of 11)



 
It's a little confusing in that there are two waiting states involved. A thread can be waiting to own the monitor, or it can be waiting inside a wait() call.

wait() is used when a thread discovers that it can't proceed until some condition changes, e.g. a thread might wait when it discovers a queue is empty (or full). Other threads might synchronize on and check the same queue while the first one is in wait(). When a thread, say, adds an item to an empty queue it uses notify() or notifyAll() to wake a thread that might be waiting to be fed. Because adding the item and doing the notify should be done in the same monitor there's no danger that the nofity migh happen between the consumer thread finding the queue empty and it calling wait().
 

Posts:536
Registered: 6/29/03
Re: Two questions about thread and lock.  
Jul 6, 2004 7:21 AM (reply 9 of 11)



 
Thanks for your kindly reply, malcolmmc buddy!

I am puzzled about the following statements you mentioned,

Because adding the item and doing
the notify should be done in the same monitor there's
no danger that the nofity migh happen between the
consumer thread finding the queue empty and it calling
wait().

Can you give me a more detailed description?

Best regards,
George

 

Posts:6,147
Registered: 11/9/00
Re: Two questions about thread and lock.  
Jul 6, 2004 7:37 AM (reply 10 of 11)



 
I'm trying to explain why both wait and notify must be done inside sychronized sections and the test and wait must be inside one monitor and the change and notify also in a montor.

Supossing you put:

if(queue.isEmpty())     sychronized(queue)        queue.wait();


Then the other thread might cut in between the test and the wait, put an item on the queue and do a notify. If the notify happens before this thread does a wait then it won't work and the consumer thread will wait even though there's now an item on the queue.

What you should do is put:

synchronized(queue) {     while(queue.isEmpty())         queue.wait();     item = queue.remove(0);   }


Which means the other thread can't get the monitor to do the notify between the test and the wait (and no other thread can swipe the item before this thread does).

Similar problems could happen if the thread putting items on the queue didn't lock the queue between adding the item and doing the notify.
 

Posts:536
Registered: 6/29/03
Re: Two questions about thread and lock.  
Jul 6, 2004 8:57 AM (reply 11 of 11)



 
Thanks, malcolmmc buddy!

You have cleared all my doubts!

Best regards,
George

 
This topic has 11 replies on 1 page.