• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

two question about Thread iteraction

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry l got the point .
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic