Skip to content

Simplified Learning Blog

Learning made easy

  • Home
  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
  • Java Interview Questions
    • Top 50 Core Java Interview Questions & Answers (2026 Edition)
    • Top 20 Spring Boot Interview Questions for Freshers (2026 Edition): The Ultimate Cheat Sheet
    • Top 40+ Multithreading interview questions
    • Top 10 AWS Lambda interview questions
  • Java Thread Tutorials
    • How to create thread in Java
    • Multithreading in java
    • Daemon Thread in Java | How to create daemon thread in java
    • Java Virtual Threads (Project Loom) in Real Enterprise Applications
    • WebFlux vs. Virtual Threads in 2026: The Senior Architect’s Decision Matrix
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • Practical System Design Examples using Spring Boot, Queues, and Caches (2026 Guide)
    • System Performance Objective
  • Spring Boot Tutorial
    • Spring Boot Rest API Example complete guide
    • Spring MVC vs. Spring Boot in 2026: The Senior Architect’s Definitive Guide
    • Spring Boot Application.properties vs. YAML: The 2026 Architect’s Verdict
  • Core Java Deep Dives
    • Java int to String Conversion: Performance Benchmarks & Memory Pitfalls
    • String to Integer Conversion in Java | Java convert string to int
    • Converting PDF to JSON in Java Top 3 ways to code:
    • Calculate date of birth from age in jquery
    • How to convert excel to PDF using java
    • jcalendar in java swing example
    • Series program in java
  • Tools
    • JSON Formatter & Debugging Guide for Spring Boot Developers
    • Free Character Counter Tool: The Ultimate Guide to Counting Characters, Words, and Text Statistics
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

How to create thread in Java

Posted on February 12, 2023April 27, 2023 By Govind No Comments on How to create thread in Java

Ways to create thread in Java

In this tutorial, we are going to see how to create a thread in Java. There are two ways to create threads in Java.

Table of Contents

Toggle
  • Ways to create thread in Java
  • How to create thread in Java
    • Create a Thread in Java using Thread Class
    • Using Runnable interface
  • Creating Thread using Java 8
  • Types of Threads in java

1. Using Thread Class

2. Using Runnable interface

How to create thread in Java

Create a Thread in Java using Thread Class

We can create using Thread class. Java provides thread class to create threads by extending this class. Please see below code snippet.

package com.slb.thread;

public class ThreadDemoUsingExtend  extends  Thread{

    @Override
    public void run() {
        System.out.println("Running Thread....");
    }
}

Here, in the above code snippet, you can see we are extending a class called ‘Thread’ and overriding the run method which will execute once we call the start() method on that thread.

 public static void main(String[] args) {
        ThreadDemoUsingExtend threadObject = new ThreadDemoUsingExtend();
        threadObject.start();
    }

Here, we have created the thread class object named “threadObject” inside main method and calling the start() method on it, which will call the run() method.

Output:

Running Thread....
Process finished with exit code 0

Note that, the start() method call (threadObject.start()) returns as soon as we call it, it will not wait until the run() method is done.

Using Runnable interface

There is another way to create threads in Java using the java.lang.runnable interface provided by Java. The runnable interface has only one method, i.e. run(). Look at the interface below.

package java.lang;
//before java 8
public interface Runnable {
    void run();
}

//After java 8 it is functional interface as

package java.lang;

@FunctionalInterface
public interface Runnable {
    void run();
}

We will create a thread and implement this interface as shown below:

package com.slb.thread;

public class ThreadB implements Runnable{
    @Override
    public void run() {
        System.out.println("in run() of ThreadB class which implements Runnable interface");
    }
}

In the above code snippet, we have created one class and implemented the runnable interface, which has run() method which will execute the code inside it.

There is another way to implement by creating anonymous class.

public class ThreadDemo {

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("running...");
            }
        };
        runnable.run();
    }
}

Creating Thread using Java 8

From Java 8 and above, you can also use lambda to create a runnable implementation. Runnable is a functional interface in java. Please see below example of using lambda expression that implements runnable :

public class ThreadJava8 {

    public static void main(String[] args) {

        Runnable runnable = () -> {
            System.out.println("inside run method of lambda expression");
        };
    }
}

In the above code snippet, you can see we are using a lambda expression which automatically provides implementation of run method inside its expression.

Types of Threads in java

There are two types of threads in java

  1. User Threads 2. System Thread

User threads are normal threads created by users, and system threads are daemon threads created by system or even user can make any thread as daemon.

Checkout to know more about Daemon Threads | Checkout Multithreading here

Checkout the complete code used here at SLB-GitHub.

Govind

For over 15 years, I have worked as a hands-on Java Architect and Senior Engineer, specializing in building and scaling high-performance, enterprise-level applications. My career has been focused primarily within the FinTech, Telecommunications, or E-commerce sector, where I’ve led teams in designing systems that handle millions of transactions per day.

Checkout my profile here : AUTHOR https://simplifiedlearningblog.com/author/

Related

Java Threads

Post navigation

Previous Post: Java Thread Tutorials
Next Post: Daemon Thread in Java | How to create daemon thread in java

More Related Articles

Daemon Thread in Java | How to create daemon thread in java Java Threads
Top 40+ Multithreading interview questions Java Threads
WebFlux vs. Virtual Threads in 2026: The Senior Architect’s Decision Matrix Java Threads
Spring Boot Rest API Example complete guide Java Threads
Java Thread Tutorials Java Threads
Multithreading in java Java Threads

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Testing Asynchronous Flows with Awaitility: The End of Flaky Tests
  • Migrating from Java 8/11 to Java 25: The Refactoring Checklist (2026 Edition)
  • How to Handle Errors in Spring Boot REST APIs (2026 Guide): The Global Exception Handling Pattern
  • Practical System Design Examples using Spring Boot, Queues, and Caches (2026 Guide)
  • Spring Boot Application.properties vs. YAML: The 2026 Architect’s Verdict

Recent Comments

  1. Govind on Performance Principles of Software Architecture
  2. Gajanan Pise on Performance Principles of Software Architecture
Simplified Learning

Demystifying complex enterprise architecture for senior engineers. Practical guides on Java, Spring Boot, and Cloud Native systems.

Explore

  • Home
  • About Us
  • Author Profile: Govind
  • Contact Us

Legal

  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
© 2026 Simplified Learning Blog. All rights reserved.
We use cookies to improve your experience and personalize ads. By continuing, you agree to our Privacy Policy and use of cookies.

Powered by PressBook Green WordPress theme