Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question from nikos

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is the output even coming? i thought after wait there is no one to notify the thread to continue
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't figure out.

Btw, what does thread.wait() at line 18 actually means. Is it about asking the main thread to wait for thread created. If so, can the main thread be asked to wait for other thread?

Thanks.

_charles

 
Martin Sergeant
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand what is going on
Calling thread.wait() will cause the main thread to pause until another thread calls thread.notify()
As no other thread does, you would expect the program to hang
This is indeed the case if wait is called on a another object
e.g.

will cause the program to hang

A clue to what is going on, however is that 999999 is always the output, so the main
thread does wait but only until the Job thread has completed.
Therefore maybe when thread has run its course it may call notify on itself?

Martin
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Therefore maybe when thread has run its course it may call notify on itself?

Notify on itself or main, because I guess main is waiting for thread

Correct me if I'm wrong.

Thanks

_charles
 
Henry Wong
author
Marshal
Pie
Posts: 20999
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are seeing is an implementation detail of the join() method, and is not guarantee to always behave like this.

The way join() is implemented, is that it checks to see if the joing thread is alive, and if it is still alive, it will call wait() on the Thread object for the joining thread. To make this work, one of the actions that is done, when a thread terminates is that it calls notifyAll() on the thread object... so if you also call wait() on the thread object, just like the join() method, you too, will receive a notification.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic