Java Intermediate

0% completed

Previous
Next
Java Thread Class

The Thread class in Java is a fundamental component for achieving multithreading. It represents a single unit of execution and provides methods to create, start, and manage threads. By extending the Thread class, you can create custom threads with their own behavior by overriding the run() method. This lesson covers the key features, methods, and syntax of the Thread class, along with a practical example.

Image

Creating a Thread by Extending the Thread Class

When you extend the Thread class, you override the run() method to specify the code that should be executed in a new thread. Once the thread object is created, you call its start() method to begin execution.

Syntax

public class MyThread extends Thread { @Override public void run() { // Code to be executed in the new thread } }
  • Explanation:
    • MyThread is a subclass of Thread.
    • The run() method is overridden to define the specific task for the thread.
    • Calling start() on an instance of MyThread will create a new thread and invoke the overridden run() method.

Key Methods of the Thread Class

Below is a table summarizing some of the most important methods in the Thread class:

MethodDescription
start()Initiates the execution of the thread by calling the run() method in a new thread of execution.
run()Contains the code that constitutes the new thread’s task; should be overridden to define the thread's behavior.
sleep(long millis)Causes the current thread to pause execution for a specified number of milliseconds.
join()Causes the current thread to wait until the thread on which it is called terminates.
interrupt()Interrupts a thread, which may cause it to stop sleeping or waiting, depending on its state.
isAlive()Returns true if the thread is still running or has not yet terminated.

Example 1: Creating and Running Threads

In this example, we define a custom thread class by extending Thread. Two threads are created and started, each printing its name and a counter value while pausing briefly between iterations.

Java
Java

. . . .

Example Explanation:

  • Defining MyThread: A nested class MyThread extends Thread and overrides the run() method to print a message five times with a one-second pause between prints.
  • Creating Threads: Two thread instances (thread1 and thread2) are created in the main method.
  • Starting Threads: Calling start() on each instance launches the threads concurrently. The run() method executes in each thread, printing the current thread's name and a count.
  • Custom Thread Execution: The custom thread class MyThread overrides run() to print the thread name and a count from 1 to 5.
  • Use of sleep(): Within run(), Thread.sleep(1000) pauses the thread for 1 second between iterations, simulating a delay in execution.
  • Concurrent Execution: Two instances of MyThread are started concurrently, so their outputs may interleave as they run in parallel.

Example 2: Using Thread Methods (sleep() and join())

This example demonstrates how to use the sleep() and join() methods. One thread sleeps between operations, and the main thread waits for it to complete before proceeding.

Java
Java

. . . .

Example Explanation:

  • Defining SleepyThread: The SleepyThread class extends Thread and overrides the run() method to print a count three times, pausing for two seconds between prints.
  • Starting and Joining: In the main method, an instance of SleepyThread is created and started.
    The join() method is called on the thread, causing the main thread to wait until SleepyThread completes its execution.
  • Resuming Main Thread: After SleepyThread finishes, the main thread resumes and prints a concluding message.

Understanding the Thread class and its methods lays the foundation for more advanced topics in Java multithreading, such as synchronization, avoiding race conditions, and managing thread priorities.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next