• 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

Thread getting out of waiting state... How?

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

As this is my first post here, I want to congratulate you smart people out there for your results in the Certification exams and thank you for always be willing to help noobs like myself!

Now... The question...
Well... While formulating it, I understood what I did wrong: I synchronized on the wrong instance. However, I still don't understand what happens in the following (kind of nonsense) code:



From what I understand from KS&BB's book, a thread gets out of waiting state if and only if a notify() (on the object it waits for) is performed.
The main thread waits for t, but, surprisingly, gets moving exactly when it should (although there is no visible notify; I searched in Thread class also) and prints the factorial variable.
Is there a call to notify() somewhere in the Thread's automatic treatment? Can you figure out what happens there?

Thanks,
Valentin.
 
Valentin Musoiu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel kind of useless, as I found a possible answer 2 pages later

KS&BB:

There's also a possible situation called spontaneous wakeup that may exist in some
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.)

 
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

Valentin Musoiu wrote:I feel kind of useless, as I found a possible answer 2 pages later

KS&BB:

There's also a possible situation called spontaneous wakeup that may exist in some
situations—a thread may wake up even though no code has called notify()
or notifyAll(). (At least, no code you know about has called these methods.
Sometimes the JVM may call notify() for reasons of its own, or code in some other
class calls it for reasons you just don't know.)



Well, in this case, the "other" code is related to the mechanism that implements thread join(). The thread class implements join() by checking the alive flag, and waiting on the Thread instance, if it is not. The cleanup code for threads will issue a notifyAll(), on the Thread object, after the thread is no longer alive -- to wakup the threads waiting to join. And since your thread is also waiting on the Thread object, it will wake up too.

Henry
 
Valentin Musoiu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Henry!
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic