99+
Search...
Java Course Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading
Multithreading in Java
Last Updated : 11 Apr, 2025
Multithreading is a Java feature that allows the concurrent execution of two
or more parts of a program for maximum utilization of the CPU. Each part of
such a program is called a thread. So, threads are lightweight processes
within a process.
Different Ways to Create Threads
Threads can be created by using two mechanisms:
1. Extending the Thread class
2. Implementing the Runnable Interface
1. By Extending the Thread Class
We create a class that extends the [Link] class. This class
overrides the run() method available in the Thread class. A thread begins its
life inside the run() method. We create an object of our new class and call
the start() method to start the execution of a [Link]() invokes the run()
method on the Thread object.
Example: Creating a Thread by extending the Thread class.
// Java code for thread creation by extending
// the Thread class
class Multithreading extends Thread {
public void run()
{
try {
// Displaying the thread that is running
[Link](
"Thread " + [Link]().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
[Link]("Exception is caught");
}
}
}
// Main Class
public class Geeks
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Multithreading object
= new Multithreading();
[Link]();
}
}
}
Output
Thread 13 is running
Thread 14 is running
Thread 12 is running
Thread 11 is running
Thread 16 is running
Thread 15 is running
Thread 18 is running
Thread 17 is running
2. By Implementing the Runnable Interface
We create a new class which implements [Link] interface and
override run() method. Then we instantiate a Thread object and call start()
method on this object.
Example: Demonstrating the multithreading by implementing the Runnable
interface.
// Java code for thread creation by implementing
// the Runnable Interface
class Multithreading implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
[Link](
"Thread " + [Link]().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
[Link]("Exception is caught");
}
}
}
// Main Class
class Geeks {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Thread object
= new Thread(new Multithreading());
[Link]();
}
}
}
Output
Thread 12 is running
Thread 15 is running
Thread 18 is running
Thread 11 is running
Thread 17 is running
Thread 13 is running
Thread 16 is running
Thread 14 is running
Thread Class vs Runnable Interface
Aspect Thread Class Runnable Interface
Inheritance We need to extend a Thread It is a interface so use
class. Because it is a class. It using the implent
means we can not add any keyword and allows us to
other class if we extend it. use other classes as well.
Thread Sharing The Thread object cannot be The object of Runnable
sharing among the different interface can be shared
multiple threads. among different threads.
Aspect Thread Class Runnable Interface
Implementation We can override the run() We can implement the
method present in the run() method present in
Thread class. the Runnable interface.
Built-in It provides methods like These methods are not
Methods yield(), interrupt() and getId present in the Runnable
etc. interface .
Comment More info Next Article
Lifecycle and States of a Thread in
Campus Training Program Java
Similar Reads
Java Multithreading Tutorial
Threads are the backbone of multithreading. We are living in the real world
which in itself is caught on the web surrounded by lots of applications. With…
15+ min read
Java Thread Priority in Multithreading
Java being Object-Oriented works within a Multithreading environment in
which the thread scheduler assigns the processor to a thread based on the…
15+ min read
Java Multithreading Interview Questions and Answers
Java Multithreading lets developers run multiple tasks at the same time, making
apps faster and more responsive. Java is used by over 10 million developers o…
15+ min read
Main thread in Java
Java provides built-in support for multithreaded programming. A multi-
threaded program contains two or more parts that can run concurrently. Each…
12 min read
Java Features
Java is a high-level, object-oriented programming language. It is known for its
platform independence, reliability, and security. Java Programming language…
15+ min read
Java Thread Class
Thread is a line of execution within a program. Each program can have multiple
associated threads. Each thread has a priority which is used by the thread…
15+ min read
Java Threads
Threads are lightweight subprocesses, representing the smallest unit of
execution with separate paths. The main advantage of multiple threads is…
15+ min read
Thread Pools in Java
Background Server Programs such as database and web servers repeatedly
execute requests from multiple clients and these are oriented around…
15+ min read
Inter-thread Communication in Java
Inter-thread communication in Java is a mechanism in which a thread is paused
from running in its critical section, and another thread is allowed to enter (or…
15+ min read
Synchronization in Java
In Multithreading, Synchronization is crucial for ensuring that multiple threads
operate safely on shared resources. Without Synchronization, data…
15+ min read
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
Company Explore Languages DSA Data Science & Web
About Us Job-A-Thon Hiring Python Data Structures ML Technologies
Legal Challenge Java Algorithms Data Science With HTML
Privacy Policy GfG Weekly C++ DSA for Beginners Python CSS
Careers Contest PHP Basic DSA Data Science For JavaScript
In Media Offline Classroom GoLang Problems Beginner TypeScript
Contact Us Program SQL DSA Roadmap Machine Learning ReactJS
GfG Corporate DSA in JAVA/C++ R Language DSA Interview ML Maths NextJS
Solution Master System Android Tutorial Questions Data Visualisation NodeJs
Placement Design Competitive Pandas Bootstrap
Training Program Master CP Programming NumPy Tailwind CSS
GeeksforGeeks NLP
Videos Deep Learning
Python Computer DevOps System Design School Databases
Tutorial Science Git High Level Design Subjects SQL
Python GATE CS Notes AWS Low Level Design Mathematics MYSQL
Programming Operating Systems Docker UML Diagrams Physics PostgreSQL
Examples Computer Kubernetes Interview Guide Chemistry PL/SQL
Django Tutorial Network Azure Design Patterns Biology MongoDB
Python Projects Database GCP OOAD Social Science
Python Tkinter Management DevOps Roadmap System Design English Grammar
Web Scraping System Bootcamp
OpenCV Tutorial Software Interview
Python Interview Engineering Questions
Question Digital Logic
Design
Engineering Maths
Preparation More Tutorials Machine Programming Clouds/ GATE 2026
Corner Software Learning/Data Languages Devops GATE CS Rank
Development Booster
Company-Wise Science C Programming DevOps
Recruitment Software Testing with Data Engineering GATE DA Rank
Complete Machine
Process Product Structures AWS Solutions Booster
Learning & Data
Aptitude Management C++ Programming Architect GATE CS & IT
Science Program -
Preparation Project Course Certification Course - 2026
[LIVE]
Puzzles Management Java Programming Salesforce GATE DA Course
Data Analytics
Company-Wise Linux Course Certified 2026
Training using
Preparation Excel Python Full Course Administrator GATE Rank
Excel, SQL, Python
All Cheat Sheets Course Predictor
& PowerBI - [LIVE]
Data Science
Training Program -
[LIVE]
Data Science
Course with IBM
Certification
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved