Java Programming Tutorials

Java programming tutorials with many code examples!

Java Thread Join Example


How to Java Thread join to do simple synchronization of threads? In this post we show an example where one thread waits for another to clean up after it. Read on!


When a couple of threads have to do work sequentially we can guarantee their order in a few ways. One way could be to use Single Thread Executor that can serialize threads. In the below example we’re going to show how to do that using Thread.join() method, which is sufficient for simple cases.

The Thread.join() method when called from one thread, stops it, and waits for the other thread to die. In the example we have a BusyJob thread that we want to run first and a JobCleaner thread that we want to run after the busy job has finished. In the main thread we start JobCleaner thread, which will start BusyJob thread – both threads could be started from the main thread, but we implemented it this way. Also in main thread we are waiting for the cleaner to finish and then we end the program.


import java.util.concurrent.TimeUnit;

public class ThreadJoinExample {

    private static class BusyJob extends Thread {
        public void run() {
            System.out.println("Started BusyJob...");
            try {
            } catch (InterruptedException e) {
                // ignore
            System.out.println("Busy job done.");

    private static class JobCleaner extends Thread {

        private Thread job;

        public JobCleaner(Thread job) {
            this.job = job;

        public void run() {
            System.out.println("Cleaner is waiting on a busy job...");
            try {
                System.out.println("Cleaning up after job: "
                                   + job.getClass().getSimpleName());
            } catch (InterruptedException e) {
                System.out.println("Some other thread has interrupted me!");
            System.out.println("Cleaning done.");

    public static void main(String[] args) {
        Thread busyJob = new BusyJob();
        Thread cleaner = new JobCleaner(busyJob);

        System.out.println("Starting the cleaner.");

                + " is waiting for the cleaner to finish");
        try {
        } catch (InterruptedException e) {
            System.out.println("main thread interrupted!");

        System.out.println("End of main thread!");

The join method has three variants:

  • void join()
    It’s the same as join(0)
  • void join(long millis)
    It waits for given number of milliseconds for this thread to die.
  • void join(long millis, int nanos)
    It waits for given number of milliseconds plus nanoseconds for this thread to die.

And here’s the result of running the above example:

Starting the cleaner.
main is waiting for the cleaner to finish
Cleaner is waiting on a busy job...
Started BusyJob...
Busy job done.
Cleaning up after job: BusyJob
Cleaning done.
End of main thread!

As you can see all threads have executed in order as expected. This method of serialization is fine for simple cases like this one, but for more threads it may get complicated very fast and then better use Single Thread Executor.

Was it helpful? Share!