# Thread Methods in Java (with example)

In 
Published 2022-12-03

This tutorial will explain the main thread methods we have in Java.

Multithreading refers to two or more tasks executing concurrently within a single OS process. A thread is an independent path of execution within a process. Many threads can run concurrently within a process. There can be multiple processes inside the OS.

There are two ways to create thread in java:

  • by implementing the Runnable interface (java.lang.Runnable)
  • by extending the Thread class (java.lang.Thread)

Here are some examples of using Thread methods in Java.

Please take a look at the comments in the code.

package com.exampe.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) throws InterruptedException {

    SpringApplication.run(DemoApplication.class, args);

    System.out.println("--------------------------------------------");

    System.out.println("A Thread is not returning any value; it implements Runnable.");

    // Create a simple thread using lambda expression
    Thread t1 = new Thread(() -> {
      for (int i = 1; i <= 5; i++) {
        System.out.println("Message from Thread #1");
        if (i == 1) {
          // Receive the real/internal name of the current thread
          System.out.println("The internal name of the thread #1 is "+Thread.currentThread().getName());

          // Change the real/internal name of the current thread
          Thread.currentThread().setName("NewName");

          System.out.println("The new name of the thread #1 is "+Thread.currentThread().getName());

          // This method suspend the thread for mentioned time duration in argument (sleeptime in ms)
          try {
            Thread.sleep(3000);
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }

          //Rarely used: Tell to the CPU that this thread is not doing something particularly important
          //and if any other threads or processes need to be run, they should. Otherwise, the current thread
          //will continue to run.
          Thread.yield();

          // Priority range : 1 (minimum) to 10 (maximum)
          System.out.println("The priority of the thread #1 is "+Thread.currentThread().getPriority());

          // Get the state of the Thread
          System.out.println("The priority of the thread #1 is "+Thread.currentThread().getState());

        }
        try {
          Thread.sleep(200);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
    });

    // Will start the thread t1
    t1.start();

    // Wait for the t1 thread to finish its work
    t1.join();
    // The following lines of code will be executed when t1 thread has finished its work
    System.out.println("--------------------------------------------");
  }
}

And here is the result of the execution:

--------------------------------------------
A Thread is not returning any value; it implements Runnable.
Message from Thread #1
The internal name of the thread #1 is Thread-1
The new name of the thread #1 is NewName
The priority of the thread #1 is 5
The priority of the thread #1 is RUNNABLE
Message from Thread #1
Message from Thread #1
Message from Thread #1
Message from Thread #1
--------------------------------------------

Process finished with exit code 0