Software Development

Mastering the Main Thread in Java


Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java, renowned for its robust multi-threading capabilities, revolves around the concept of threads that execute concurrently. At the core of every Java application lies the main thread. This thread orchestrates the program’s entry point, initiates other threads, and manages the application’s lifecycle. This programming tutorial dives into the world of the main thread, exploring its role, nuances, and best practices for optimal thread management.

Java Thread lifecycle


Java Thread Lifecycle

What is the Main Thread in Java?

The main thread is the linchpin of every Java application. When a Java program starts up, it is the main thread that executes the main method, which serves as the starting point for the program’s execution.

While the main thread is responsible for getting things started, it does not exist in isolation. It has the power to spawn additional threads, enabling parallel execution of tasks, enhancing performance, and ensuring a responsive user experience.

Read: Top Online Courses to Learn Java

Controlling the Main Thread in Java

Controlling the main thread’s behavior using the sleep() and join() methods is a common technique in Java programming. These methods allow you to introduce delays or synchronization points in the execution of the main thread.

How to Use Java’s sleep() Method

The sleep() method is part of the Thread class and allows developers to pause the execution of a thread for a specified amount of time. It is particularly useful for introducing delays or simulating time-consuming tasks. Here is a code example showing how to use Java’s sleep() method and its syntax:

public class SleepExample {
  public static void main(String[] args) {
    System.out.println("Main thread is starting.");

    try {
      // Sleep for 2 seconds
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("Main thread resumed after sleep.");
  }
}

In the above program, the main thread sleeps for 2 seconds (2000 milliseconds) using the Thread.sleep() method. The InterruptedException is a checked exception that can occur when another thread interrupts the sleeping thread.

Using the join() Method in Java

Java’s join() method is also part of the Thread class and is used for synchronization purposes. It allows one thread to wait for the completion of another thread. This is particularly useful when programmers want to ensure that certain tasks are completed before the main thread continues execution.

In the following code example, the main thread starts a worker thread and then uses the join() method to wait for the worker thread to complete. This ensures that the worker thread finishes its execution before the main thread continues. Here is an example:

public class JoinExample {
  public static void main(String[] args) throws InterruptedException {
    Thread workerThread = new Thread(() -> {
      System.out.println("Worker thread is starting.");
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Worker thread has completed.");
    });

    workerThread.start();

    // Wait for the worker thread to complete
    workerThread.join();

    System.out.println("Main thread resumes after worker thread.");
  }
}

How to Combine sleep() and join()

You can also combine the sleep() and join() methods to introduce both time-based delays and synchronization points in your program. Here is some example code to demonstrate:

public class SleepJoinCombo {
  public static void main(String[] args) throws InterruptedException {
    Thread workerThread = new Thread(() -> {
      System.out.println("Worker thread is starting.");
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Worker thread has completed.");
    });

    workerThread.start();

    // Wait for the worker thread to complete or max 3 seconds
    workerThread.join(3000);

    System.out.println("Main thread resumes after worker thread or timeout.");
  }
}

In this example, the main thread starts the worker thread and then waits for the worker thread to complete within a maximum of 3 seconds. If the worker thread does not finish within that time frame, the main thread continues execution regardless.

For more, check out our tutorial: How to Pause Thread Execution in Java.

Java Thread Safety and the Main Thread

Thread safety is a fundamental concern in multi-threaded programming. The main thread often interacts with other threads, necessitating synchronization mechanisms to prevent data corruption and inconsistencies. Consider this example:

public class ThreadSafetyExample {
  private static int sharedCounter = 0;

  public static synchronized void incrementCounter() {
    sharedCounter++;
  }

  public static void main(String[] args) throws InterruptedException {
    Thread thread1 = new Thread(() -> {
      for (int i = 0; i < 1000; i++) {
        incrementCounter();
      }
    });

    Thread thread2 = new Thread(() -> {
      for (int i = 0; i < 1000; i++) {
        incrementCounter();
      }
    });

    thread1.start();
    thread2.start();

    thread1.join();
    thread2.join();

    System.out.println("Final counter value: " + sharedCounter);
  }
}

In this example, the incrementCounter method is synchronized using the synchronized keyword. This ensures that only one thread can execute the method at a time, preventing concurrent modifications to sharedCounter.

You can learn more about thread safety in our tutorial: Java Thread Safety.

Java Main Thread in GUI Applications

In graphical user interface (GUI) applications, the main thread takes on added significance. It manages user interactions and updates the UI. However, performing time-consuming operations in the main thread can lead to UI unresponsiveness. To avert this, lengthy tasks should be offloaded to worker threads:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUIThreadExample {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Main Thread in GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton startButton = new JButton("Start Task");
    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);

    startButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        startButton.setEnabled(false);

        Thread workerThread = new Thread(() -> {
          for (int i = 0; i < 10; i++) {
            final int count = i;
            SwingUtilities.invokeLater(() -> {
              textArea.append("Task in worker thread: " + count + "\n");
            });

            try {
              Thread.sleep(1000);
            } catch (InterruptedException ex) {
              ex.printStackTrace();
            }
          }

          SwingUtilities.invokeLater(() -> {
            startButton.setEnabled(true);
          });
        });

        workerThread.start();
      }
    });

    frame.add(startButton, BorderLayout.NORTH);
    frame.add(new JScrollPane(textArea), BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
  }
}

This example showcases a GUI application using Swing. When the “Start Task” button is clicked, a worker thread is launched to perform a time-consuming task. The SwingUtilities.invokeLater() method ensures safe UI updates from the worker thread.

Final Thoughts on Java’s Main Thread

Mastering the main thread in Java is essential for building responsive, efficient, and robust applications. By understanding its role, synchronization techniques, and best practices for thread management, developers can unlock the full potential of multi-threading in Java.

From simple console applications to complex GUIs and server-side systems, the main thread’s orchestration forms the bedrock of concurrent programming in Java. Embrace the power of threads, synchronize wisely, and create high-performance applications that delight users and elevate Java programming.

Next Steps

Now that you have a firm grasp of how the main thread works in Java, we recommend checking out some of our other tutorials discussing threads, concurrency, and multi-threading in Java: