Last Updated : 23 Jul, 2025
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. The slave thread is created as a result of the main thread. This is the last thread to complete the execution.
A thread can programmatically be created by:
You can create threads by implementing the runnable interface and overriding the run() method. Then, you can create a thread object and call the start() method.
Creating Threads in JavaThere are multiple ways to create threads in Java:
1. Thread ClassThe Thread class provides constructors and methods for creating and operating on threads. The thread extends the Object and implements the Runnable interface.
Method:
public void start()
It starts a newly created thread. Thread moves from new state to runnable state when it gets a chance, executes the target run() method
Use Case: Suitable for simple applications for creating a thread that performs a specific task.
Implementation:
Java
// Extending Thread in Class
import java.io.*;
// Class inheriting Thread class
class MyThread extends Thread
{
// Overriding the run method
@Override
public void run(){
System.out.print("Welcome to GeeksforGeeks.");
}
}
class Geeks
{
public static void main(String[] args)
{
// Creating thread
MyThread t1 = new MyThread();
// Starting thread
t1.start();
}
}
Welcome to GeeksforGeeks.2. Runnable Interface
Any class with instances that are intended to be executed by a thread should implement the Runnable interface. The Runnable interface has only one method, which is called run().
Use Case: It is suitable for cases where you want to separate the task from the thread management. Also, Runnable is a interface so allows us to perform multiple inheritance.
Implementation:
Java
// Implementing Runnable Interface
import java.io.*;
// Class inheriting Runnable interface
class RunnableClass implements Runnable
{
// Override run method
@Override
public void run()
{
System.out.println("Inside run method");
}
}
class Geeks
{
public static void main(String args[])
{
// Create an object of Runnable target
RunnableClass gfg = new RunnableClass();
// Pass the runnable reference to Thread
Thread t = new Thread( gfg , "gfg");
// Start the thread
t.start();
// Get the name of the thread
System.out.println(t.getName());
System.out.println(t.currentThread().getName());
}
}
gfg main Inside run method3. Using Lambda Expressions
Lambda Expressions are very useful for creating the Threads in the cases when the operations to be performed are limited.
Use Case: Used for Threads performing simple tasks.
Implementation:
Java
// Using Lambda Method to Create Thread
import java.io.*;
class Geeks
{
public static void main (String[] args)
{
// Lambda Thread Created
Thread thread1 = new Thread(() -> {
// Operations Performed for thread1
System.out.println("Lambda Thread running");
});
// Running the Thread
thread1.start();
}
}
Lambda Thread running4. Using ExecutorService (for Managing Thread Pools)
The ExecutorService framework provides a higher-level way to manage threads. It allows you to create a pool of threads and manage their execution.
Use Case: Used for concurrent execution of multiple threads.
Implementation:
Java
// Using ExecutorService
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Geeks
{
public static void main(String[] args)
{
// Create a thread pool with 2 threads
ExecutorService threads = Executors.newFixedThreadPool(2);
// Submitting Threads as Runnable
threads.submit(() -> {
System.out.println("Task 1 is running in " + Thread.currentThread().getName());
});
threads.submit(() -> {
System.out.println("Task 2 is running in " + Thread.currentThread().getName());
});
// Shutdown the threads/executor
threads.shutdown();
}
}
Task 1 is running in pool-1-thread-1 Task 2 is running in pool-1-thread-2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4