• 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

About notify() & notifyAll().. Thank you

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot understand the difference between notify() and notifuAll()

Look at this example in the book:

Since notify() only affect one of the waiting threads, why the result is:
waiting for the result...
waiting for the result...
waiting for the result...
Total: 4950
Total: 4950
Total: 4950

Thanks a lot


[HENRY: Added Code Tags]
[ November 26, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yu Sun!

This seems to be your first posting, so

Welcome to the Ranch!




Sorry, I cannot answer your question. I'm having a little bike tour around the Alster lake (position 53� 33.65' N, 10� 00.00'E).
Perhaps some fresh air will revitalize my brain.

But at least I found out that the code gives the same result when you have no notify at all.
I put some comments in the code, put an extra input line in (identifying the thread) and removed the "Food".


Prints:
Thread-1 waiting for the result...
Thread-2 waiting for the result...
Thread-3 waiting for the result...
Total: 4950
Total: 4950
Total: 4950

By the way, if you use this code tags on your you don't lose the indentations.




Yours,
Bu.

---
Yu, your member number is a prime. Did you notice that?
[ November 26, 2006: Message edited by: Burkhard Hassel ]
 
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
Hmmm... The last time I tried to explain a similar example, I believe the original poster, and a few others, thought I was lying...


This is due to a conflict with the code used by the join() method. When a thread exits, the resources for the thread will be cleaned up, the alive flag will be cleared, and a notifyAll() is send on the thread object. The reason this last task is done, is so that any thread trying to join() with the thread can wake up.

The join() method uses the thread object as the notification object to wait on. This is purely an implementation detail -- and should not be relied upon. Future implementations may fix this issue.

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

the fresh air didn't help me but Henry's posting did. Thanks.
Henry Wong posted November 26, 2006 06:38 AM

Hmmm... The last time I tried to explain a similar example, I believe the original poster, and a few others, thought I was lying...



As you co-wrote a book about threads (*) I think, I'd better believe you.
Other topic: The thing on the cover of this thread book - is it from Ernst Haeckel's book about marine organisms (~year 1850) ?


Yours,
Bu.

(*) I hope it sells like hell!
 
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
Burkhard,

You have an internet connection on your bike? I'm impressed ...

No idea on the marine creature on the cover. I was trying to get a Wombat -- because my wife thinks they're soooo cuuuuute !!! -- but got overridden.

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,
Henry Wong posted November 26, 2006 09:59 AM

Burkhard,

You have an internet connection on your bike? I'm impressed ...


Oh, it's just integrated in the radar antenna of the anti-missile system.

No idea on the marine creature on the cover. I was trying to get a Wombat -- because my wife thinks they're soooo cuuuuute !!! -- but got overridden.

I find bush babies even nicer.
Perhaps they wanted a tentacle bearing beast in relation to threads. By the way, I just looked it up, there is a reprint of Haeckels drawings (which I didn't know).
amazon link
Is it true that the other Autor didn't like the language being renamed to Java from it's original name, oak?

Back to threads.
I modified the code a bit to get an output on the alive status of the calculator thread:
With the last line being notifyAll(); it prints (at least on my system)
Thread-1 waiting for the result...
Thread-2 waiting for the result...
Thread-3 waiting for the result...
Total: 4950 - sync thread object calculator-thr. isAlive:true
Total: 4950 - sync thread object calculator-thr. isAlive:true
Total: 4950 - sync thread object calculator-thr. isAlive:true





with notify();
Thread-1 waiting for the result...
Thread-2 waiting for the result...
Thread-3 waiting for the result...
Total: 4950 - sync thread object calculator-thr. isAlive:true
Total: 4950 - sync thread object calculator-thr. isAlive:false
Total: 4950 - sync thread object calculator-thr. isAlive:false



And without a notify:
Thread-1 waiting for the result...
Thread-2 waiting for the result...
Thread-3 waiting for the result...
Total: 4950 - sync thread object calculator-thr. isAlive:false
Total: 4950 - sync thread object calculator-thr. isAlive:false
Total: 4950 - sync thread object calculator-thr. isAlive:false

So with notifyAll the calculator thread notified all before itself was terminated (and before it sends this "terminating notify" as described by Henry). Must be still living when sending this notifyAll.
With notify() it only notified one waiting thread (as it should be) and the other notifies came from this cleaning up process, indicated by the fact that these threads were already dead. And without notify at all, all notifies came from this clean up.

I see dead threads...


Yours,
Bu.
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic