• Post Reply Bookmark Topic Watch Topic
  • New Topic

Purpose of join()

 
praveen kumaar
Ranch Hand
Posts: 443
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose their are n number of threads and i have a variable count now i want each thread to increment it by some specific number of times.now i have started each thread through a loop and then after that again i invoke join() on each thread via a loop.what is the difference if i donot invoke join method.i will not use a synchronization for count in order to point error.it doesn't seems that join will remove the error.then what is a need for it..

i understand join as-if their is a thread t and i invoke join on it then it will pause all other threads execution except t.is it right?
 
Vibhor Rawat
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,

First- for your question- "i understand join as-if their is a thread t and i invoke join on it then it will pause all other threads execution except t.is it right?"

Join method pushes a thread into wait state (in your word- it will pause) for a specific time. However, on the same line as that of sleep (); join () is dependent on the OS for timing, so we should not assume that join will push the required thread into wait for the exact time that we specify.

For ex-



Second- If you try to use join method using a loop it will surely increment the value; however I'm not sure about the thread sequence which will do increment.
 
Henry Wong
author
Sheriff
Posts: 22519
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
i understand join as-if their is a thread t and i invoke join on it then it will pause all other threads execution except t.is it right?


No. The join() method only affects the calling thread. Your thread that calls the join() method will wait for the thread t to finish. No other threads are affected.

Henry
 
praveen kumaar
Ranch Hand
Posts: 443
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not like t.join() is:
waits for t to die i.e,if n threads are executing it will invoke a wait() on other executing threads via a loop and as t dies it will notifyAll to the threads in a wait set.
isn't it correct
 
Henry Wong
author
Sheriff
Posts: 22519
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:it is not like t.join() is:
waits for t to die i.e,if n threads are executing it will invoke a wait() on other executing threads via a loop and as t dies it will notifyAll to the threads in a wait set.
isn't it correct


Only the "waits for t to die" part is correct (meaning for the calling thread only).  It has no affect on any other thread.

Henry
 
Santosh Kumar Nayak
Ranch Hand
Posts: 149
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If join() is called on a Thread instance, the currently running thread will block until the Thread instance has finished executing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!