Java Thread Join Example

In this post, we will see about Java thread join method.

Thread class’s join() method can be used to stop current execution of thread until thread it joins, completes its task. So basically, it waits for the thread on which join method is getting called, to die.For example:
Here when you call t1.join(), main thread will wait for t1 to complete its start before resuming its execution.

Variants of join methods

There are three variant of join method

public final void join() throws InterruptedException

Thread on which join method is getting called, to die.

public final void join(long millis) throws InterruptedException

This method when called on the thread, it waits for either of following:

  • Thread on which join method is getting called, to die.
  • Specified milliseconds

public final void join(long millis,int nanos) throws InterruptedException

This method when called on the thread, it waits for either of following:

  • Thread on which join method is getting called, to die.
  • Specified milliseconds + nano seconds

💡 Did you know?

If the thread, on which join method is called, has been already terminated or has not been started yet, then join method returns immediately.

Java Thread Join example

Let’s take a simple example:

Create ThreadExampleMain.java

When you run above program, you will get following output.

Main thread execution starts
Thread 1 Start
Thread 1 end
Thread 2 Start
Thread 3 Start
Thread 2 end
Thread 3 end
Main thread execution ends

Lets analysis output now.

  1. Main thread execution starts.
  2. Thread 1 starts(Thread 1 start) and as we have put t1.join(), it will wait for t1 to die(Thread 1 end).
  3. Thread 2 starts(Thread 2 start) and waits for either 1 seconds or die, but as we have put sleep for 4 seconds in run method, it will not die in 1 second. so main thread resumes and Thread 3 starts(Thread 3 start)
  4. As we have put t2.join() and t3.join(). These 2 threads will get completed before exiting main thread, so Thread 2 will end(Thread 2 end ) and then thread 3 will end(Thread 3 end).
  5. Main thread execution ends.

Java Thread Join and Synchornization

Java thread join method guarantees happens before relationship.

It means if thread t1 calls t2.join(), then all the changes done by t2 are visible to t1.

That’s all about Java Thread Join Example.


import_contacts

You may also like:

Related Posts

  • 30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • 19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • 11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. Using Thread.sleep Sleep method causes current thread to pause for […]

  • 29 May

    How to print even and odd numbers using threads in java

    In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order […]

  • 22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • 16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.