Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats join in Thread

 
Aftab Ahmed
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi frnds,
can any buddy give complete detail of join method in Thread Object.
thanx
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
join method belongs to thread class.
It is overloaded in the following way:
i) void join()
ii) void join(long millis)
iii) void join(long millis, int nanos)
Invoking a join method in a thread means that this thread will wait for all other threads to complete their task. For example main method (main thread) may create n number of threads. When the tasks of the main thread completes it exits without waiting for the threads it created. This is the normal behavoir. Providing join in the main thread makes this thread to wait until other threads finish their task.
HTH
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharatesh,
I am just editing your message:
"Invoking a join method in a thread means that this thread will wait for all other "child" threads to complete their task. For example main method (main thread)("main thread is daemon thread" which will go to dead state only if there are no more active threads) may create n number of threads. When the tasks of the main thread completes it exits without waiting for the threads it created. This is the normal behavoir. Providing join in the main thread makes this thread to wait until other child threads finish their task."
Regards
Prasad


[This message has been edited by Prasad Ballari (edited November 23, 2000).]
 
Govinda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whaaaaaaaaaaaaat!!!???/
prasad bellari ,who told you that main thread is a Daemon Thread ???
[This message has been edited by Govinda (edited November 23, 2000).]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here's a small example of the join method of Thread class

class demo
{
public static void main(String[] args)
{
xclass x=new xclass();
Thread thread2=new Thread(x);
thread2.start();
try{
thread2.join();// current thread will wait till run of thread2 is over
}
catch(Exception e){}
System.out.println("Done");
}
}
class xclass implements Runnable
{public void run(){
int i;
for (i=0;i<10000 ;i++ )
{System.out.println(i);
}
}}

[This message has been edited by Rahul Mahindrakar (edited November 23, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic