0% completed
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.
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.
public class MyThread extends Thread { @Override public void run() { // Code to be executed in the new thread } }
MyThread
is a subclass of Thread
.run()
method is overridden to define the specific task for the thread.start()
on an instance of MyThread
will create a new thread and invoke the overridden run()
method.Below is a table summarizing some of the most important methods in the Thread
class:
Method | Description |
---|---|
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. |
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.
Example Explanation:
MyThread
extends Thread
and overrides the run()
method to print a message five times with a one-second pause between prints.thread1
and thread2
) are created in the main
method.start()
on each instance launches the threads concurrently. The run()
method executes in each thread, printing the current thread's name and a count.MyThread
overrides run()
to print the thread name and a count from 1 to 5.sleep()
: Within run()
, Thread.sleep(1000)
pauses the thread for 1 second between iterations, simulating a delay in execution.MyThread
are started concurrently, so their outputs may interleave as they run in parallel.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.
Example Explanation:
SleepyThread
class extends Thread
and overrides the run()
method to print a count three times, pausing for two seconds between prints.main
method, an instance of SleepyThread
is created and started.join()
method is called on the thread, causing the main thread to wait until SleepyThread
completes its execution.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.
.....
.....
.....