cant understand Thread join()
cant understand Thread join()
I am new to multithreading. I know the following things about threads:
thread_obj.join()
holds the thread until it over
thread_obj.join()
thread_obj.join(5000)
holds the thread for 5 sec. after then run as other thread.
thread_obj.join(5000)
I know that join()
and join(millis)
that hold thread for some time. My question is that if we provide more time in join()
then sleep()
like join(5000)
and sleep(1000)
it will run fine and thread hold for 5 sec. after then run as other thread but if you provide less time in join()
then sleep()
like, join(500)
and sleep(2000)
the join()
hold thread until it over.
join()
join(millis)
join()
sleep()
join(5000)
sleep(1000)
join()
sleep()
join(500)
sleep(2000)
join()
I have following code which I didn't understand join()
join()
public class THRD3 extends Thread {
public void run() {
for(int i = 1 ; i <= 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" : "+i);
try {
//Thread.sleep(500);
Thread.sleep(2000);
}
catch (InterruptedException e) {e.printStackTrace();}
}
}
public static void main(String args) throws InterruptedException {
THRD3 obj = new THRD3();
THRD3 obj1 = new THRD3();
THRD3 obj2 = new THRD3();
obj.start();
obj.setName("Joined Thread 0");
//obj.join(2000);
obj.join(500);
obj1.start();
obj2.start();
}
}
@Lino it is not a duplicate since OP asks for
Thread#join(long millis)
, not Threa#join()
.– Turing85
Jun 29 at 9:56
Thread#join(long millis)
Threa#join()
@Turing85 then probably this is a better duplicate: stackoverflow.com/questions/20426863/…
– Lino
Jun 29 at 9:57
Either way,
join
doesn't stop the thread. it just waits for it to complete (or the timeout whichever happens first)– Peter Lawrey
Jun 29 at 9:58
join
i know
join
hold thread for some time.for that i defining in my code– mak
Jun 29 at 10:06
join
2 Answers
2
Calling Thread.join
makes the calling thread wait for the thread to finish executing. That means it will get "blocked" on that statement and won't continue until it's done executing. Passing an argument lets you specify a timeout, which means the calling thread will wait until the thread finishes, or give up when the timeout passes.
Thread.join
In your code obj.join(500)
makes your main thread do nothing and wait until the obj
thread to finish, or until 500 milliseconds passes, whichever one happens first.
obj.join(500)
obj
well explained...
+1
– Shubhendu Pramanik
Jun 29 at 9:56
+1
see output of code after change time on sleep and join method,which i give in comment line and run...after compare both output.you will see diffrence.
– mak
Jun 29 at 9:56
.. or any other thread interrupts the current thread which will result in an InterruptedException. In this case the current thread has not waited until the timeout nor until the termination of the joined thread.
– LuCio
Jun 29 at 10:10
Your base assumption that join()
"holds" the thread in question in wrong. Thread#join()
and Thread#join(long millis)
are synchronization constructs, i.e. you use them to wait on a thread's termination.
join()
Thread#join()
Thread#join(long millis)
From the documentation of Thread#join()
:
Thread#join()
Waits for this thread to die.
An invocation of this method behaves in exactly the same way as the invocation
join(0)
From the documentation of Thread#join(long millis)
Thread#join(long millis)
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
you are right
join()
wait fot thread die and join(5000)
give atime slice to thread after run as other thread.... but please run code and, after chage time on sleep()
and join()
and after re-run and compare that both output.– mak
Jun 29 at 10:50
join()
join(5000)
sleep()
join()
just see flow of running both output you will see the problem..
– mak
Jun 29 at 10:52
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of What does this thread join code mean?
– Lino
Jun 29 at 9:55