VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-create-a-thread/

⇱ Java Program to Create a Thread - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Create a Thread

Last Updated : 30 May, 2026

A thread is a lightweight unit of execution that allows a Java program to perform multiple tasks concurrently. Java provides built-in support for multithreading, enabling efficient use of CPU resources and improved application performance.

  • Threads share the same memory and resources of a process.
  • The main thread starts automatically when a Java program begins execution.
  • New threads can be created to execute tasks independently and concurrently.

Creating Threads in Java

There are multiple ways to create threads in Java:

Thread Class

The Thread class is used to create and manage threads in Java. By extending this class and overriding the run() method, a new thread can execute a task independently.

  • Extends the Object class and implements the Runnable interface.
  • start() creates a new thread and invokes the run() method.
  • Suitable for simple thread creation tasks.

The start() method starts a new thread and moves it from the New state to the Runnable state. When scheduled by the JVM, it automatically executes the run() method.

Syntax:

class MyThread extends Thread {
public void run() {
// task to perform
}

}

MyThread t = new MyThread();
t.start();

Implementation:


Output
Welcome to GeeksforGeeks.

Runnable Interface

The Runnable interface is used to define a task that can be executed by a thread. It contains a single method, run(), which holds the code to be executed.

  • Contains only one method: run().
  • Separates task logic from thread creation.
  • Supports multiple inheritance since it is an interface.

Note: A class implements Runnable and the task runs when a thread executes its run() method.

Syntax:

class MyTask implements Runnable {
public void run() {
// task to perform
}
}

Thread t = new Thread(new MyTask());

t.start();

Implementation:


Output
gfg
main
Inside run method

Using Lambda Expressions

Lambda expressions provide a concise way to create and run threads, especially when the task is small and simple.

  • Reduces boilerplate code.
  • Best for short and simple tasks.
  • Works with the Runnable interface.

Note: Lambda expressions allow defining the thread task directly without creating a separate class.

Syntax:

Thread t = new Thread(() -> {
// task to perform
});
t.start();

Implementation:


Output
Lambda Thread running

Using ExecutorService (for Managing Thread Pools)

ExecutorService is a framework that manages and executes threads through a thread pool. It provides a more efficient and scalable approach to multithreading than creating threads manually.

  • Uses a pool of reusable threads.
  • Simplifies thread management.
  • Suitable for executing multiple tasks concurrently.

Note: ExecutorService manages thread creation and execution automatically, improving performance and resource usage.

Syntax:

import java.util.concurrent.*;
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(() -> {
// task to perform
});
executor.shutdown();

Implementation:


Output
Task 1 is running in pool-1-thread-1
Task 2 is running in pool-1-thread-2
Comment
Article Tags: