Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Notify and notifyAll

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to K&S page 753, if a method uses notify() instead of notifyAll(), only one waiting thread will be notified. How come this is not happening in the following example.



The output to the above is



I was expecting that since i used notify(), i will see one thread showing the total and three threads still waiting.

Thanks
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yea, this question comes up often, mainly because there are a few mock questions that synchronize on the Thread object. You should do a bit of searching about it.

Anyway, the reason this is happening is due to implementation detail. The Thread class join() method is implementation via calls to check if the thread is alive and calling wait() on the Thread instance if it is not. When a thread terminates, part of the cleanup is to unset the alive flag and call notifyAll() on the thread object.

So... in your example, your thread object is terminating, and hence, sending a notification to all threads waiting on the Thread object.

Henry

 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. I will admit and say that i didnt quite understand your response. I got even more confused when i tried a different example which does wait

In this example the thread does wait indefinitely. I dont understand the difference here and the previous example i gave.

 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

O. Ziggy wrote:
In this example the thread does wait indefinitely. I dont understand the difference here and the previous example i gave.



In this second example, you are waiting on an instance that is not used by another part of the program -- hence, you are not being interfered with. In the first example, you were waiting on the same instance that the core library uses to implement the Thread.join() method.

Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic