• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding locks  RSS feed

 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Do the call to yeild() or join() cauuses to release the lock ??

I know that wait() --- releases lock
sleep() --- keeps lock
notify()--- keeps lock
notifyall()--- keeps lock
join() --- ???
yeild() ---- ???

Regards,
Sharath
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the call to yeild() or join() cauuses to release the lock ??


No.

One more thing, notify and notifyAll method releases the lock which the thread has already acquired.

Naseem
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() --- releases lock
sleep() --- keeps lock
notify()--- keeps lock
notifyall()--- keeps lock
join() --- releases lock
yield() ---- keeps lock


Okay, I feel I have to explain the join() method. The fact that join() releases the lock is merely a side-effect.

Normally, when you use the join() method, you don't grab the lock, so you don't notice that join() will grab and also release the lock during its operation. Unfortunately, if you call join() while holding the thread lock, this lock will also be released. The reasoning is because the join() method calls the wait() method.

This is an implementation detail. This could be changed in future releases... So... Don't use the thread object to synchronize with, if you are using the join() method.

Henry
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

If notify() and notifyAll keeps the lock, then how the waiting thread will again acquire the lock and continue its processing.

Thanks

Naseem
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
Hi Henry,

If notify() and notifyAll keeps the lock, then how the waiting thread will again acquire the lock and continue its processing.

Thanks

Naseem


Notify() and notifyAll() are quick operations. The notification is sent, and the methods return. The wait() method will reacquire the lock, when the thread that called the notify() method releases the lock.

For example...



Henry
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notify() and notifyAll() are quick operations. The notification is sent, and the methods return. The wait() method will reacquire the lock, when the thread that called the notify() method releases the lock.


Oh is it so?... Great Thanks Henry

One more thing

As you said that the notify() method does not release the lock at the time of notification. So is it because of that the wait() method gets blocked after returning from the "waiting for lock acquisition state" as it can't get the lock at the time when it gets notified.

Thanks


Naseem
[ July 30, 2006: Message edited by: Naseem Khan ]
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be many reasons why a thread that gets the notification has to wait awhile for the lock -- a big reason is, another thread, that is entering a synchronized block, grabs it first.


In any case, I think this discussion has gone way past the certification discussion level. I think it would be best if I move this to the threads forum.

See you there...
Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!