Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusing with join() in Threads  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I'm getting confusion about join() internal implementation in Thread class. please advise.

Thread.java


My Code 1:


My Code 2 :



Here i'm getting same results with My Code 1 and My Code 2 but Question is, if we call join() OR wait() method on Thread instance,How it will get notify,when thread in wait state here as i couldn't see any notify() OR notifyAll() method in Thread class.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i understand correctly then you asked 2 questions: 1) First why join and wait method behaves like same 2) How to notify a waiting thread.

So for your 1st question: join() with no args internally calls joins(0) which then calls wait(0) and for wait() as per javadoc it is equivillant to call
wait(0). So in both the cases it is calling same methods so that's why it behaves same.

For the 2nd question, When a wait is called, monitor lock is released so that other thread can acquire it. So you can then issue
a notify on the waiting thread using the thread which just requires the lock or using main thread.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!