In Java, a Thread is a lightweight process that enables multitasking in a Java program. It is a path of execution within a program, allowing the program to perform multiple tasks simultaneously or concurrently. Each thread has its own execution context, including its own stack, but shares the process’s memory space, such as variables and methods, with other threads.
Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking.
For many readers, process-based multitasking is the more familiar form. A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently.
Important Points of Java Thread
1. Multithreading: Java allows multithreading, meaning multiple threads can run concurrently, sharing the same resources but working independently.
2. Thread Class: In Java, a thread is represented by the Thread class, which is part of java.lang package.
3. Runnable Interface: Threads can also be created by implementing the Runnable interface, which is often used to define tasks that can be run concurrently.
How to create Thread?
There are two common ways to create and run a thread in Java.
- By extending the Thread class.
- By implementing the Runnable interface.
1. Create Thread by extending the Thread class
In this method, you create a subclass of the Thread class and override its run() method, which contains the code that will be executed by the thread.
Example:
//MyThread.java file
// Example of extending Thread class
class MyThread extends Thread {
public void run() {
// Code to be executed by the thread
System.out.println("My thread is running");
}
}
public class Main {
public static void main(String[] args) {
// Creating and starting the thread
MyThread thread = new MyThread();
thread.start(); // Starts the thread, which invokes the run() method
}
}
Output:
Explanation:
- MyThread is a subclass of Thread.
- The run() method contains the code that the thread will execute.
- The start() method is called to begin the thread’s execution. This method internally calls the run() method.
2. Create Thread by the Runnable Interface
In this approach, the Runnable interface is implemented, and its run() method is overridden.
//Main.java file
// Example of implementing Runnable interface
class MyRunnable implements Runnable {
public void run() {
// Code to be executed by the thread
System.out.println("Thread is running from Runnable");
}
}
public class Main {
public static void main(String[] args) {
// Creating a Runnable object
MyRunnable myRunnable = new MyRunnable();
// Creating a thread by passing the Runnable object to the Thread constructor
Thread thread = new Thread(myRunnable);
// Starting the thread
thread.start(); // Starts the thread and invokes the run() method
}
}
Output:
Explanation:
- MyRunnable implements the Runnable interface and defines the run() method.
- The Thread class is instantiated by passing the Runnable object (myRunnable) to its constructor.
- The start() method is called to begin thread execution.
Java Thread – Questions and Answers
Q 1: What is a thread in Java?
Ans: A thread is a lightweight sub-process.
Q 2: Why use multithreading?
Ans: To improve performance and responsiveness.
Q 3: How can a thread be created?
Ans: By extending Thread class or implementing Runnable.
Q 4: What is thread lifecycle?
Ans: Different states like new, runnable, running, and terminated.
Q 5: Can multiple threads run simultaneously?
Ans: Yes, depending on CPU and OS.
Java Thread – Objective Questions (MCQs)
Q1. Which of the following is the correct way to create a thread in Java?
Q2. Which method is used to start the execution of a thread?
Q3. What will happen if we call the run() method directly instead of start()?
Q4. Which of the following methods makes the currently executing thread pause for a specified time?
Q5. Which method is used to ensure that one thread finishes before another thread starts executing?