-2

I am new to multithreading. I know the following things about threads:

  • thread_obj.join() holds the thread until it over

  • thread_obj.join(5000) holds the thread for 5 sec. after then run as other thread.

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.

I have following code which I didn't understand 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();
    }
}
13hola
  • 170
  • 1
  • 12
  • Possible duplicate of [What does this thread join code mean?](https://stackoverflow.com/questions/15956231/what-does-this-thread-join-code-mean) – Lino Jun 29 '18 at 09:55
  • 1
    @Lino it is not a duplicate since OP asks for `Thread#join(long millis)`, not `Threa#join()`. – Turing85 Jun 29 '18 at 09:56
  • @Turing85 then probably this is a better duplicate: https://stackoverflow.com/questions/20426863/thread-joinmillisecond-is-not-accuracy-in-java – Lino Jun 29 '18 at 09:57
  • 1
    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 '18 at 09:58
  • i know `join` hold thread for some time.for that i defining in my code – 13hola Jun 29 '18 at 10:06
  • @Lino i know what thread join code mean.but befour mark as duplicate please run code. and after chage time on `sleep` and `join` and after re-run and compare that output.... – 13hola Jun 29 '18 at 10:20
  • the problem is that if `sleep` time is greater then `join` time, the first thread (`obj`) will hold all thread for `join` every time. but if sleep time is less than `join` time the first thread `join` all thread only one time. – 13hola Oct 03 '18 at 07:30

2 Answers2

8

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.

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.

user464014
  • 140
  • 8
  • well explained... `+1` – Shubhendu Pramanik Jun 29 '18 at 09:56
  • 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. – 13hola Jun 29 '18 at 09: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 '18 at 10:10
3

Your base assumption that join() "holds" the thread in question in wrong. Thread#join() and Thread#join(long millis) are synchronization constructs, i.e. we use them to wait on a thread's termination.

From the documentation of 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)

Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 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. – 13hola Jun 29 '18 at 10:50