• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

SCJP/OCJP 6 (Threads)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



notify() is not called anywhere BUT waiting Thread is waking up and printing value of a
why it is happening here.
 
author
Posts: 23959
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

Tn Sharma wrote:
notify() is not called anywhere BUT waiting Thread is waking up and printing value of a
why it is happening here.



You are conflicting with the core library. Thread class instances are used internally to implement the join() mechanism. Threads that call the join() method will wait() on the Thread instance. And when a thread terminates, it will send a notification (all) on the Thread instance.

Henry
 
Tn Sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Tn Sharma wrote:
notify() is not called anywhere BUT waiting Thread is waking up and printing value of a
why it is happening here.



You are conflicting with the core library. Thread class instances are used internally to implement the join() mechanism. Threads that call the join() method will wait() on the Thread instance. And when a thread terminates, it will send a notification (all) on the Thread instance.

Henry



And what if somehow notify() is called before wait() .then waiting will infinite?
 
Henry Wong
author
Posts: 23959
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

Tn Sharma wrote:
And what if somehow notify() is called before wait() .then waiting will infinite?



Who is calling wait()? Who is calling notify()? There are multiple use-cases in context with this topic, so, which one do you mean?

Anyway...

In the case of your original example (ie. your thread calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for your thread to call wait() after the clean up code for your thread to call notifyAll(). As long as your thread is running, the environment can't clean up after it.

In the case the core library implementation of the join() method (ie. the thread that called join() calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for wait() to be called by join() after the clean up call. The implementation of join() does not do a blind wait. It will check to confirm that the thread is still alive before calling wait().

In the general case (ie. general question -- and not related to this topic). ... Notifications that are sent, when there are no threads waiting for it, are simply discarded.

Henry
 
Tn Sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Tn Sharma wrote:
And what if somehow notify() is called before wait() .then waiting will infinite?



Who is calling wait()? Who is calling notify()? There are multiple use-cases in context with this topic, so, which one do you mean?

Anyway...

In the case of your original example (ie. your thread calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for your thread to call wait() after the clean up code for your thread to call notifyAll(). As long as your thread is running, the environment can't clean up after it.

In the case the core library implementation of the join() method (ie. the thread that called join() calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for wait() to be called by join() after the clean up call. The implementation of join() does not do a blind wait. It will check to confirm that the thread is still alive before calling wait().

In the general case (ie. general question -- and not related to this topic). ... Notifications that are sent, when there are no threads waiting for it, are simply discarded.

Henry




Please observe this code
As main thread is calling wait();
and run() method on thread a is calling notify();

But as per code
main is sleeping for 5 seconds while notify() from thread a is called before 5 seconds completed .
After 5 seconds main() will call wait() and there will no notify() or notifyAll() is there.

So this main() will infinitely waiting ( tested by me)

Please differentiate the behavior of this code with original code
 
Henry Wong
author
Posts: 23959
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

Tn Sharma wrote:
Please differentiate the behavior of this code with original code



These are two different examples/cases. The first (original) case is explained already. And the second case, is explained by the "general case" in my previous post.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic