File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes how it works---join(),thread class. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "how it works---join(),thread class." Watch "how it works---join(),thread class." New topic
Author

how it works---join(),thread class.

Quratulain, azam
Greenhorn

Joined: Dec 02, 2001
Posts: 4
hello every one!
can anyone, please tell me how this join() method of the tread class works, also when it is called with parameters.
thanks.
tabinda mumtaz
Greenhorn

Joined: Mar 24, 2002
Posts: 3
AoA Annie !
Read ur call, and wanted to tell u what i found about join() method. so here it is. Do tell me is it helpful or not ?

tabinda says:
okay have u studied join() method too ?
Inam says:
ya
tabinda says:
tell me what does it do ? and why we use it ?
Inam says:
when we want a thread not to stop Before its Child thread
then we use this method
4 example
if there r child threads for main
Main should not be terminated
before its child threads
Inam says:
then we use the syntax
obj.join()
Inam says:
obj is the child thread for main
tabinda says:
yes and it will not let the main thread finish before obj right ?
Inam says:
ya absolutely
Inam says:
this is it all
tabinda says:
accha can we say that "by using join() method we don't have to put the main thread to call sleep() method to finish last after its child threads terminate becoz join() method takes care of that "
tabinda says:
tell me is the above statement correct in ur opinion ?
Inam says:
absolute abt join().
Inam says:
its right 2 say like this
tabinda says:
good
Inam says:
n its a better way than sleep()
tabinda says:
now tell me one more thing regarding join(long millis)
tabinda says:
what does it do ?
tabinda says:
does it keeps the main thread from finishing for only the particular amount of time specified in the block ?
tabinda says:
i mean in the parameter block of this method call
Inam says:
ya
Inam says:
ya it stops main thread from termination for the time specified as argument
tabinda says:
but then don't u think its better to use join() becoz it never lets the main thread die before its child thread
Inam says:
ya u r right its better 2 use join() rather than join(long mili sec)
--------------------------------------------------

Now another reference from a java site :

join() method. This blocks the calling thread until the thread whose join() method is called is terminated. And my dear friend u must have guessed that the calling thread is none other than main thread itself. So main method is blocked until child thread terminates.
join([timeout])
Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates - either normally or through an unhandled exception - or until the optional timeout occurs.
* When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).
* A thread cannot join itself because this would cause a deadlock.
* It is an error to attempt to join() a thread before it has been started.

Hope that it will help u out. take care
Allah Hafiz !
tabinda
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1952
HERE IS YOUR ANSWER:
Q. What does the Thread.join() method do? Can you give an example?
[ March 24, 2002: Message edited by: Roseanne Zhang ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Roseanne's example, unfortunately, does demonstrate the semantics of join() but not what it's good for, as the main thread merely waits until the job thread has finished. You could just as well do all the work in the main thread.
Job threads and join() are especially powerful when there two or more I/O-bound things have to happen which can be done in parallel. You can then "fork" the path of execution and do everything at the same time. You use Thread.join to join the teeth of the fork back together again at the end (to stretch the analogy a bit ).Does that help?
- Peter
PS. Thanks for the code Roseanne
 
 
subject: how it works---join(),thread class.
 
Threads others viewed
Finding a prime number
join() method of class Thread
Thread class join() method
is it a DeadLock Condition?
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com