๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Thread Class in Java  


Introduction
  • Thread is the pre-defined class in Java which is used to create and manage threads (independent paths of execution).
  • Thread class is present in the java.lang package.
  • Syntax :-
    package java.lang;
    
    public class Thread extends Object implements Runnable
    {
        // Fields
        // Constructors
        // Methods
    }
  • Threads are used to perform multitasking in Java programs.
    • Handling multiple client requests simultaneously (Servers)
    • Performing background tasks (file download, DB query, etc.)
    • Improving performance by parallel execution

Constructors of Thread Class:
  • Below are the constructors of Thread class with examples:
Constructor Description
Thread() Creates a new thread with no target and a generated name.
Thread(Runnable target) Creates a new thread that will execute the specified Runnable object.
Thread(Runnable target, String name) Creates a new thread with a target task and a custom name.
Thread(String name) Creates a new thread with a custom name but no target.
Thread(ThreadGroup group, Runnable target) Creates a new thread inside a specified thread group with a target task.
Thread(ThreadGroup group, Runnable target, String name) Creates a new thread inside a group with a target task and custom name.
Thread(ThreadGroup group, Runnable target, String name, long stackSize) Creates a new thread with group, target, name, and custom stack size.
  • Below is the program for constructors of Thread class:
    • // Demonstration of Thread class constructors
      class MyRunnable implements Runnable
      {
          public void run()
          {
              System.out.println(Thread.currentThread().getName() + " is running...");
          }
      }
      
      public class ThreadConstructorsDemo
      {
          public static void main(String[] args)
          {    
              // 1. Default constructor
              Thread t1 = new Thread();
              System.out.println("Thread t1 (default): " + t1.getName());
      
              // 2. Thread(Runnable target)
              Thread t2 = new Thread(new MyRunnable());
              t2.setName("RunnableThread");
              t2.start();
      
              // 3. Thread(Runnable target, String name)
              Thread t3 = new Thread(new MyRunnable(), "Worker-1");
              t3.start();
      
              // 4. Thread(String name)
              Thread t4 = new Thread("HelperThread");
              System.out.println("Thread t4 (only name): " + t4.getName());
      
              // 5. Thread(ThreadGroup group, Runnable target)
              ThreadGroup g1 = new ThreadGroup("GroupA");
              Thread t5 = new Thread(g1, new MyRunnable());
              t5.setName("GroupThread-1");
              t5.start();
      
              // 6. Thread(ThreadGroup group, Runnable target, String name)
              ThreadGroup g2 = new ThreadGroup("GroupB");
              Thread t6 = new Thread(g2, new MyRunnable(), "GroupThread-2");
              t6.start();
      
              // 7. Thread(ThreadGroup group, Runnable target, String name, long stackSize)
              ThreadGroup g3 = new ThreadGroup("GroupC");
              Thread t7 = new Thread(g3, new MyRunnable(), "GroupThread-3", 1024);
              t7.start();
          }
      }

Methods of Thread Class:
  • Below are some common and important methods of Thread class:
Method Description / Use More
start() Starts the execution of the thread (internally calls run()).
run() Contains the code that will be executed by the thread.
currentThread() Returns a reference to the currently executing thread object.
isAlive() Checks whether the thread is still running.
getName() Returns the name of the thread.
setName(String name) Sets or changes the name of the thread.
isDaemon() Checks whether the thread is a daemon thread.
setDaemon(boolean on) Marks the thread as a daemon thread or a user thread.
getPriority() Returns the priority of the thread (1 to 10).
setPriority(int newPriority) Changes the priority of the thread (1 = MIN, 5 = NORM, 10 = MAX).
sleep(long millis) Makes the currently executing thread pause for the specified time.
yield() Pauses the current thread and allows other threads of equal priority to execute.
join() Makes one thread wait for another thread to finish execution.
interrupt() Interrupts a thread if it is waiting, sleeping, or blocked.
isInterrupted() Checks whether the thread has been interrupted.
interrupted() Checks whether the current thread has been interrupted and clears the interrupt flag.
  • These all methods are explained deeply in next tutorials.