This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads - join() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads - join()" Watch "Threads - join()" New topic
Author

Threads - join()

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Hello friends!
Lets take a thread. Could you tell me how to join() this thread to "main" thread?
Thanks!
Richard Parker
Ranch Hand

Joined: Jan 23, 2007
Posts: 70
Hi Anna,

I'm glad that helped - yes, invoking static methods with instance variables can get confusing - especially if you don't know (or forget) that method is static! yikes!

Correct me if I'm wrong - but I'm pretty sure the static method that matches the *reference* type's class will be invoked, rather than the actual object that reference is pointing to.

Ex:
SuperClass ref = new SubClass();
ref.staticMethod(); -------------> invokes SuperClass.staticMethod();

SubClass ref = new SubClass();
ref.staticMethod(); -------------> invokes SubClass.staticMethod();


Thanks,
Richard


"...it takes all the running you can do to keep in the same place. <br />If you want to get somewhere else, you must run at least twice as fast as that!" <br />~ Through the Looking-Glass
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

the API about join() in class Thread is a bit short, it just says:
Waits for this thread to die.


So if you have somewhere
Thread x = new ThreadExtedingClass();
x.join();

the current thread stops, until thread who called join() is ready (and dead).

If you want a thread to join the main thread, make the thread call join inside the main method:

Prints:
Main started,
main ready.
0123456789.


Note, if you start the thread before joining, it may already be dead. In this case there would be nothing to join.

Also note, that in most cases (runtime environments) the whole try catch block virtually does nothing, as main would be finished anyway when the thread not even has started to run.

Yours,
Bu.


all events occur in real time
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Wow!! Thanks a lot for your reply and that great example.
I will try the code and post questions, if any.
Thanks again!
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Can we join() main thread from AAA's run()?
Also, join() without start()? Unable to get the logic.
Thanks in adv!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi BurkHard,

Thanks for nice explanation.

main thread (it is the current running thread) join(s) the
thethread, so why is "main ready" printed before thethread
completes.

As you said, if you start a thread before joining, the thread
may be dead so there is nothing to join. But I dont see that in
your example. If main joins the thethread, it should not proccede
until the thread completes.

"Waiting eagerly for your reply" or anybody else please

Is is 100% guaranteed output what come in Barkhard's example.

Regards,

cmbhatt


cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Chandra wrote:
As you said, if you start a thread before joining, the thread may be dead so there is nothing to join. But I dont see that in your example.


Yes, it is not in my example to avoid confusion. But just remove the line theThread.start(); from the code posted above and insert it before the try catch block with the join().
In this case, theThread is already dead (because it is so short) when the main thread comes to the line with the join(). The output shows no joining then, theThread's output will be there before main is ready:
Main started,
0123456789.
main ready.

Faisal asked:
Can we join() main thread from AAA's run()?

Yes. Be aware, that an instance of Thread calls join. Therefore you need a reference to the instance of the main thread inside the run method to join it.
I changed the Thread class, that you can provide a reference to the main thread via its constructor:



I inserted a sleep into main this time, because, again, without, main would have been dead before the join happens.

Output:

Maybe try out the code without the sleep() in the main method.

Be aware that you cannot specify what thread has to wait with join(). It's always the current thread that will wait for the join-calling thread to complete.


Yours,
Bu.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
1- Main waits for theThread to die before it(main) could proceed. Right?
2- You mean, the code after theThread.start() is very short so before run
could execute main dies.

Please elaborate.
I am scratching my head "If main calls theThread.join()(waits for theThread to die), why last statement of main finishes before run() method of the theThread runs"

Am I gone mad? Asking mad question?
if (true) {
pleaseElabotate();
}else {
pleaseElaborate();
}


Thanks,
cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
1- Main waits for theThread to die before it(main) could proceed. Right?

Perhaps right, perhaps not.
Should be "current thread waits..."
Not sure what main thread you are refering to.


2- You mean, the code after theThread.start() is very short so before run could execute main dies.
No, misunderstanding. I meant the code in the run method of AAA is so short, that the AAA thread has already started, run and died, before main reaches the line with the join().
Very often, eg in applications with windows(Frames), the main thread dies within the first second, while the running thread with the user interface is alive and kicking.

Am I gone mad?
I do not have an MD in psychiatry, but I guess you are suffering from a desease that is called THREAD (Traumatic Hypermania Related Encephalitis Associated Dissociation)

Take two aspirins and stay in bed.


Yours,
Bu.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355



Well, the only thing we need is "main" thread object. Alright, even if we get it, how to pass it to the other thread? Simple! Pass it through constructor!! Thanks a ton Burkhard Hassel! You gave me that idea. Thanks again!
Could you please check the above code and tell me if I'm right?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Faisal,
yes, all right with your runnable, but again, main method may also be already dead when the thread from the runnable reaches the line with the join.
Perhaps include a sleep in both loops to see the difference with/without join().
And perhaps put
System.out.println("main lives: " + thread.isAlive());
immediately before the join() call and see what happens.


Yours,
Bu.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Perhaps, putting the other thread(in our example, Thread-N) sleep() is not needed. Because, join() itself puts the thread on hold - exactly like sleep().
Yup, you are absolutely right, before join() it is a good idea to test whether the thread still exists. However, if the thread we wish to join terminated already, there won't be any use of join().
Thank you very much. I learned a lot with your help. Please do see my other questions too. Thanks again.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Burkhard,

Bruce Eckel says: (Author of Thinking in Java)


One thread may call join() on another thread to wait for
the second thread to complete before proceeding. If a thread
calls t.join() on another thread t,then the calling thread
is suspended until the target thread t finishes (when
t.isAlive() is false.


I am very alleviated from the problem of THREAD (Traumatic Hyper...).

What I guess is, if your very first example when your main thread (main thread I mean because you call theThread.join() from the main method),
joins to the theThread which is not alive at that moment so according to
Bruce, main wont join it.
It(join()) is only effective when you start the thread and then join:
like:



I am happy with the output:
main starts
false
true
output of the run method of the theThread
main ready



The correct explanation of the code would be (as I think), main thread (Burkhard, I think I hope you get me what main I am talking about) calls
the join() on the theThread and theThread is alive so main thread joins to the theThread; and Chandra Bhatt is happy with the output.



Correct me, and tell at what extent I have alleviated from the THREAD.


Thanks,
cmbhatt
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Hey bhatt!
see my code above, you will understand much more.
Hth,
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Yeah Faisal,

I got a good idea, how things go on, join() has no impact if the
thread, the current thread is joining to is already dead.

Passing main thread to the other one to wait for is good idea
to have better understanding of main thread and other one versus
join() method of the Thread class.

Good!

Thanks,
cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

at the beginning of this ehem... thread, I made a mistake.
As Chandra wrote
theThread which is not alive at that moment so according to Bruce, main wont join it.

That's right, my idea of first join and then start is not the very best idea...


In class Thread you can read, what join() does. It calls the overloaded method join(long) with join(0), without timeout.
Method join(long l) just calls wait(), but only if (or better while) the thread is alive:

and when a thread hasn't started yet it is not alive.


Bu.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Oh yeah,

Now I have majority of vote regarding the join() and above all is the actual fact.

Thanks Bu!
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Hey guys!
Don't think too much and get yourselves complicated.
My basic idea is very simple one! Lets take 2 threads A and B, ok? Let's start them. A.start() and B.start(). Now, I've decided, for example, to join thread B to A - which means, "Dear thread B, please wait until thread A finishes off." So, I would write, A.join() inside B's run() method, right?
Similarly, I thought to do same with main thread, which starts with main() method. So, my question was, how would I actually call main thread? Well, after observing Bu's code, I learnt, simply pass the main-thread-object using Thread.currentThread(). After all, we need thread object right? Once we get main-thread-object, we can do all things as we noramlly do with any thread-object.
Hope this logic helps and serves you playing with threads!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Before the current working thread could join there should be something live to join. That is why it is good idea to start the thread before the current working thread could call t.join() to suspend until the thread t completes.
If the thread say t is not alive (only just created, start() method is not called yet), t.join has no effect on that. Whom to join (main() thread says in our example).



Regards,
cmbhatt
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

main thread starts as soon as you enter the main() method. So, we need not test whether it's alive or not. We don't say main().start right?
However, if we are joining some other thread, as you said, it will be good to test whether its alive or not.
What would happen if the other thread is not alive? Let me check...
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Faisal,

In my concern, we didn't go to check the main() thread whether it is alive or not; in our ongoing talk main() thread join()s to the thread we just created but is not started, therefore no concern, main() joins it. A thread can join to the alive thread.

What example you showed, where you passed currentThread() to the constructor, so that it could join to the thread (main() thread) you passed to it, was really good. I got an idea from that.

What do you say?


Thanks,
cmbhatt
[ April 17, 2007: Message edited by: Chandra Bhatt ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Faisal wrote:
main thread starts as soon as you enter the main() method. So, we need not test whether it's alive or not. We don't say main().start right?
However, if we are joining some other thread, as you said, it will be good to test whether its alive or not.


Yes, but the main thread can already be dead.


prints
main ready
0123456789main lives: false


Yours,
Bu.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

"The non-static join() method of class Thread lets one thread "join onto the end" of another thread. If you have a thread B that can't do its work until another thread A has completed its work, then you want thread B to "join" thread A. This means that thread B will not become runnable until A has finished (and entered the dead state)."

Similarly, if you want to "join" some thread(say thread C) to main thread, that means, thread C will not become runnable until main thread has finished and entered the dead state. I hope, you got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads - join()