Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • 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
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter and Validator
  • 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 Admin 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.

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

Top 40+ Multithreading interview questions Java Threads
Java Thread Tutorials Java Threads
Daemon Thread in Java | How to create daemon thread in java Java Threads
Spring Boot Rest API Example complete guide 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

  • Top 50 Java Coding Interview Questions and Answers (2025 Updated)
  • Java Record Class Explained: Simple, Immutable Data Carriers
  • Java Convert int to String – 5+ Methods with Examples
  • String to Integer Conversion in Java | Java convert string to int
  • PDF to JSON Convertor

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook Green WordPress theme