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

two question about Thread iteraction

 
salih ayan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
salih ayan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry l got the point .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic