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

Java Thread Tutorials

Posted on February 12, 2023February 27, 2024 By Admin No Comments on Java Thread Tutorials

What is Thread in Java

In this Java Thread Tutorials, we are going to learn about threading Java. Threads are the basic building block of Java programming. Every program has at least one thread called main thread. Basically, every process runs on either single or multiple threads. A single thread is nothing but a light weight and smallest unit of processing.

Table of Contents

Toggle
  • What is Thread in Java
  • Thread Life Cycle:
  • Example of thread
  • Java Thread Class Methods
  • Java Thread Example

To work with threading, Java provides a class called Thread class. Every thread has a life cycle :

Thread Life Cycle:

java-thread-tutorials
Thread Life Cycle

Every thread goes through these above life cycle processes.

New: In this state, a thread is created using Thread class, and it will remain in new state until the program starts the thread.

Runnable: Once a thread is ready to run, it moved into runnable state. Depends on the scheduler, it is scheduled to run at any time.

Running: When Thread gets CPU, it moves to running state and back to running when it deallocates CPU.

Waiting/Blocked: When a thread is temporally in waiting state, it will be moved it either waiting/blocked state.

Terminated/Dead: A thread is in terminated state when, it exists on the completion of the code execution given to it and in case of error scenario.

Thread Example:

Let’s create a thread in Java.

Example of thread

package com.slb.thread;

public class ThreadDemo {

    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println(thread.getName());
        System.out.println(thread.getState());
        System.out.println(Thread.currentThread().getState() + "--" + Thread.currentThread().getName() );
    }
}

Output:

Thread-0
NEW
RUNNABLE--main

Process finished with exit code 0

In the above example, we have created a thread called “thread” using new Thread() class. Then we are printing the name of the thread and the state of the thread. As shown in the output of the execution, we can see that Thread-0 is the name of the newly created thread and New is the current state of that thread.

In Java, main() is also a thread. It’s a daemon thread which runs in the background. In the above example, we are getting the current thread using Thread.currentThread() method, and we are printing its state and name. As you can see in the output, it’s RUNNABLE and main is the name of the thread.

Java Thread Class Methods

Will see some of the methods in the thread class:

1.run(): This method is the entry point which is used to start execution of the thread.

2.start(): Starts a new thread by calling run() of Thread/runnable object

3.sleep(): This method suspends the thread for a specified time specified. Syntax sleep(int duration). Duration is in milliseconds.

4.yeild(): This method is used to pause the execution temporarily and let other threads to execute.

5.join(): This method is used to permits one thread to wait until the other thread to finish its execution.

6.isAlive(): This method will check if thread is alive or dead.

Java Thread Example

Example:

package com.slb.thread;

public class ThreadMethodDemo extends Thread {

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


    public static void main(String[] args) {
        ThreadMethodDemo threadMethodDemo_ONE = new ThreadMethodDemo();
        ThreadMethodDemo threadMethodDemo_TWO = new ThreadMethodDemo();

        threadMethodDemo_ONE.setName("SLB-Thread-1");
        threadMethodDemo_TWO.setName("SLB-Thread-2");


        threadMethodDemo_ONE.start();
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main thread is running here....");
        threadMethodDemo_TWO.start();
    }
}

In the above example, we have created two thread objects, named threadMethodDemo_ONE and threadMethodDemo_TWO, and we are setting the name of these two thread objects as SLB-Thread-1 and SLB-Thread-2. We are calling the start() method on first thread as threadMethodDemo_ONE.start(); it will call the run() method and execute the block of statements present inside run().

Then we are calling sleep() method on the current thread (main thread which is running) with 10000 milliseconds i.e. 10 seconds, so the program execution will wait for 10 seconds before executing the next lines of instruction. Note that sleep method throws interrupted exception, so we need to throw it or handle it properly.

Next Topic: Creating Threads | Ways to create threads in Java | how to create thread in Java

Also Explore Multithreading in Java.

Related

Java Threads

Post navigation

Previous Post: JSON Formatter and Validator
Next Post: How to create thread in Java

More Related Articles

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