aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread wakes (get lock) without notify() or notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread wakes (get lock) without notify() or notifyAll()" Watch "Thread wakes (get lock) without notify() or notifyAll()" New topic
Author

Thread wakes (get lock) without notify() or notifyAll()

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 415
Hi!

If a thread calls wait, it is supposed to be waiting :] until it be awake in another thread using notify() or notifyAll(), but if all other threads finished running, then it will be awake (get the lock) automatically.

Am I right?
armando fonseca
Ranch Hand

Joined: Apr 03, 2009
Posts: 49
Leandro Coutinho wrote:Hi!

If a thread calls wait, it is supposed to be waiting :] until it be awake in another thread using notify() or notifyAll(), but if all other threads finished running, then it will be awake (get the lock) automatically.

Am I right?

Yes and No, In most cases the scheduler will give priority to a runnable thread, but in reality is the JVM thread scheduler will based it decision in its scheduling algorithm, and not all scheduling algorithm in java behave the same, so is better not assume what you mentioned. For example, what happen that instead of one thread waiting , there are 10 thread, all of them runnable, which thread will the JVM choose? Remember that you can have an scheduling algorithm based on priorities , or fifo order, who knows.

The good news that for the SCJP, you don't need to worry about low level things!

cheers,
Armando


scjp6-90%
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Technically, you shouldn't rely on a thread waiting forever if no other thread calls notify() on the object the first thread called wait() on. Also, you shouldn't rely on a thread waking up if no other thread calls notify() on the object. I know this sounds weird, but:

1. In theory, you would imagine that if a thread calls wait() on an object, and no other thread calls notify() on the same object, the thread that is waiting will continue waiting indefinitely.

2. In practice, the JVM sometimes will wake up a waiting thread for no reason (K&B call this spurious wakeups in their book.) This is one of the reasons why you should enclose any wait() invocations in a while loop which checks for a relevant condition to make sure that whatever the thread was waiting for has actually happened.


All code in my posts, unless a source is explicitly mentioned, is my own.
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 415
thanks armando and Ruben!
xD
Welly Tambunan
Ranch Hand

Joined: Aug 18, 2008
Posts: 40
Ruben Soto wrote:This is one of the reasons why you should enclose any wait() invocations in a while loop which checks for a relevant condition to make sure that whatever the thread was waiting for has actually happened.


so i think that's the pattern when we're working on thread. Can you give a reference book about the pattern on multithreading Ruben ?
Thank you berfore.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Welly Tambunan wrote:
Ruben Soto wrote:This is one of the reasons why you should enclose any wait() invocations in a while loop which checks for a relevant condition to make sure that whatever the thread was waiting for has actually happened.


so i think that's the pattern when we're working on thread. Can you give a reference book about the pattern on multithreading Ruben ?
Thank you berfore.

Hi Welly,

K&B discuss this in their book in Chapter 9. Basically what you do is something like:

while (!condition) {
object.wait();
}

The condition could be something like !object.isEmpty() if you are waiting to consume an element which is produced by another thread and which is deposited on object (a collection.)

I hope that answers your question.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread wakes (get lock) without notify() or notifyAll()
 
Similar Threads
Threads notify() and notifyAll()
NX: URLYBird / my approach of the reading problem
Is notifyAll good enough?
Logical/File Locking and nested synchronisation
notify and notify all