• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks armando and Ruben!
xD
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!