A Java thread is the smallest unit of execution within a program. It is a lightweight subprocess that runs independently but shares the same memory space as the process, allowing multiple tasks to execute concurrently.
Create Threads in Java
We can create threads in java using two ways
Thread-Creation- Extending Thread Class
- Implementing a Runnable interface
1. By Extending Thread Class
Create a class that extends Thread. Override the run() method, this is where you put the code that the thread should execute. Then create an object of your class and call the start() method. This will internally call run() in a new thread.
Java
import java.io.*;
import java.util.*;
class MyThread extends Thread{
// initiated run method for Thread
public void run(){
String str = "Thread Started Running...";
System.out.println(str);
}
}
public class Geeks{
public static void main(String args[]){
MyThread t1 = new MyThread();
t1.start();
}
}
OutputThread Started Running...
2. Using Runnable Interface
Create a class that implements Runnable. Override the run() method, this contains the code for the thread. Then create a Thread object, pass your Runnable object to it and call start().
Java
import java.io.*;
import java.util.*;
class MyThread implements Runnable{
// Method to start Thread
public void run(){
String str = "Thread is Running Successfully";
System.out.println(str);
}
}
public class Geeks{
public static void main(String[] args){
MyThread g1 = new MyThread();
// initializing Thread Object
Thread t1 = new Thread(g1);
// Running Thread
t1.start();
}
}
OutputThread is Running Successfully
Note: Extend Thread when when you don’t need to extend any other class. Implement Runnable when your class already extends another class (preferred in most cases).
Life Cycle of a Thread
During its thread life cycle, a Java thread transitions through several states from creation to termination.
Life-Cycle- New: Thread object is created but not started.
- Runnable: Thread is ready to run and waiting for CPU allocation.
- Running: Thread is executing its run() method.
- Waiting/Blocked: Thread waits for a resource or another thread.
- Terminated: Thread completes execution or is stopped.
Running Threads in Java
There are two methods used for running Threads in Java:
- run() Method: Contains the code for the thread. Calling it directly behaves like a normal method call.
- start() Method: Launches a new thread and internally calls run() concurrently.
Example: Using Thread Class and Runnable Interface
Java
// Thread class implementation
class ThreadImpl extends Thread{
@Override
public void run(){
// Output: Thread Class Running
System.out.println("Thread Class Running");
}
}
// Runnable interface implementation
class RunnableThread implements Runnable{
@Override
public void run(){
// Output: Runnable Thread Running
System.out.println("Runnable Thread Running");
}
}
public class Geeks{
public static void main(String[] args){
// Create and start Thread class thread
ThreadImpl t1 = new ThreadImpl();
t1.start();
// Create and start Runnable interface thread
RunnableThread r = new RunnableThread();
Thread t2 = new Thread(r);
t2.start();
// Wait for both threads to complete
try {
t1.join(); // Wait for t1
t2.join(); // Wait for t2
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
OutputThread Class Running
Runnable Thread Running
Note: We use start() to launch a new thread, which then calls the run() method in parallel. If we call run() directly, it works like a normal method call and no new thread is created.
Java Thread Class
The Thread class is used to create and control threads in Java. Each object of this class represents a single thread of execution.
Syntax
public class Thread extends Object implements Runnable
Advantages of Threads
- Improved performance: Multiple threads can execute tasks concurrently.
- Better resource utilization: Threads share the same memory and resources.
- Responsive applications: UI applications remain responsive while performing background tasks.
Related Articles
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java