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 two question about Thread iteraction 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 "two question about Thread iteraction" Watch "two question about Thread iteraction" New topic
Author

two question about Thread iteraction

salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
l want to ask you about below two sentences

First (my mind is confused.What does just below sentence want to express )
For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object.




And second just below sentence.l have an opinion.Please check whether l am right ;;;
--> when any thread execute the wait() metod of target object that thread will start waiting and in this situation another threads can not enter the synchronized block normaly
but in this case the thread which is currently waiting notification from target object, temporarily releases the lock and now in this situation other threads also can enter the synchronized block
and also if other threads execute wait() method they also start to wait for notification.

When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution.




please help.
Thanks again
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Hi to all.

My mind is confused, what K&B book want to express in the below part


When the thread waits, it temporarily releases the lock for other threads to use, but
it will need it again to continue execution. It's common to find code like this:


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){}
}




what does book mean , it means;

"waiting thread sometimes temporarily releases the lock for other threads to use synchronized block"

till here we already know that any thread in the synchronized block will not release the lock until they finish own job in the synchronized code.

please clear me.


Thanks to all.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39



It may be a good idea to tell us what you are confused with. Or maybe tell us what you do understand. At this point, there is not enough clarity to give you a quick answer -- besides pointing you parts of the Oracle tutorial on threads.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Henry Wong wrote:

It may be a good idea to tell us what you are confused with. Or maybe tell us what you do understand. At this point, there is not enough clarity to give you a quick answer -- besides pointing you parts of the Oracle tutorial on threads.

Henry


Hi l meant that in K&B book "Thread Interaction" part .Book explains this topic with a simple example.Which is just below.




and than adding this part which confused my mind


When the thread waits, it temporarily releases the lock for other threads to use, but
it will need it again to continue execution.
It's common to find code like this:


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){}
}



The point that book tells "When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution" made my mind confused.
Let me explain you what l understand from this sentence(When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution)
Check me whether l am right.
This sentence want to express that ;

The thread which is waiting between synchronized blocks for notification (in this case other threads can not enter this synchronized block ) it temporarily releases the lock without ending
its job in the synchronized block.

we already know that any thread in the synchronized block will not release the lock until they finish own job in the synchronized code. is not it?


Thaks for your answers
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

salih ayan wrote:
and than adding this part which confused my mind...


When the thread waits, it temporarily releases the lock for other threads to use, but
it will need it again to continue execution.


The point that book tells "When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution" made my mind confused.
Let me explain you what l understand from this sentence(When the thread waits, it temporarily releases the lock for other threads to use, but it will need it again to continue execution)
Check me whether l am right.

This sentence want to express that ;

The thread which is waiting between synchronized blocks for notification (in this case other threads can not enter this synchronized block ) it temporarily releases the lock without ending
its job in the synchronized block.

we already know that any thread in the synchronized block will not release the lock until they finish own job in the synchronized code. is not it?


Yes... The wait() method call will release the lock, thus allowing other threads to acquire the lock. However, once it gets a notification (and wakes up), it must reacquire the lock, in order to return from the wait() method.

Henry
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
Thanks Henry l got the point .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: two question about Thread iteraction
 
Similar Threads
wait and notify
Syntax for wait/notify
about threads, using locks of the objects in synchronized blocks
Wait and Notify
Every object can have a list of threads