GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • equals()
  • finalize()
  • getClass()
  • hashCode()

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Thread Class - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Thread Class

Last Updated : 9 Apr, 2026

A thread is the smallest unit of execution within a program, allowing multiple tasks to run concurrently. In Java, threads help improve performance by enabling parallel execution.

  • Created using the Thread class or Runnable interface
  • Starts execution with start() which internally calls run()
  • Enables concurrent execution of multiple tasks

Note: Every class that is used as a thread must implement Runnable interface and override its run method.

Syntax

public class Thread extends Object implements Runnable

Constructors of Thread Class

Constructor Action Performed 
Thread()Allocates a new Thread object.
Thread(Runnable target)Allocates a new Thread object.
Thread(Runnable target, String name)Allocates a new Thread object.
Thread(String name)Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target)Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target, String name)Allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by a group.
Thread(ThreadGroup group, Runnable target, String name, long stackSize)Allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.
Thread(ThreadGroup group, String name)Allocates a new Thread object.

Example: Java program to demonstrate usage of Thread class 

Java
class MyThread extends Thread 
{
  	// Overriding the run method
  	@Override
    public void run() 
    {
        for (int i = 0; i < 5; i++) 
        {
            System.out.println(Thread.currentThread().getName()
                               + " - Count : " + i);
            
          	try {
              	// Sleep for 500 milliseconds
                Thread.sleep(500); 
            } 
          	catch (InterruptedException e) {
                System.out.println("Thread interrupted");
            }
        }
    }
}

// Main Class
public class Geeks 
{
    public static void main(String[] args) 
    {
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();

        thread1.setName("Thread 1");
        thread2.setName("Thread 2");

      	// Start thread 1
        thread1.start(); 
      
      	// Start thread 2
        thread2.start(); 
    }
}

Output:

Thread 1 - Count : 0
Thread 2 - Count : 0
Thread 1 - Count : 1
Thread 2 - Count : 1
Thread 1 - Count : 2
Thread 2 - Count : 2
Thread 1 - Count : 3
Thread 2 - Count : 3
Thread 2 - Count : 4
Thread 1 - Count : 4

Note: Execution of the thread is not according to the sequence, it can be executed in any sequence Thread 1 then Thread 2 , or Thread 2 then Thread 1.

Methods of Thread Class

MethodsAction Performed 
activeCount()Returns an estimate of the number of active threads in the current thread's thread group and its subgroups
checkAccess()Determines if the currently running thread has permission to modify this thread
clone()Throws CloneNotSupportedException as a Thread can not be meaningfully cloned
currentThread()Returns a reference to the currently executing thread object
dumpStack()Prints a stack trace of the current thread to the standard error stream
enumerate(Thread[] tarray)Copies into the specified array every active thread in the current thread's thread group and its subgroups
getAllStackTraces()Returns a map of stack traces for all live threads
getContextClassLoader()Returns the context ClassLoader for this Thread
getDefaultUncaughtExceptionHandler()Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception
getId()Returns the identifier of this Thread
getName()Returns this thread's name
getPriority()Returns this thread's priority
getStackTrace()Returns an array of stack trace elements representing the stack dump of this thread
getState()Returns the state of this thread
getThreadGroup()Returns the thread group to which this thread belongs
getUncaughtExceptionHandler()Returns the handler invoked when this thread abruptly terminates due to an uncaught exception
holdsLock(Object obj)Returns true if and only if the current thread holds the monitor lock on the specified object
interrupt()Interrupts this thread
interrupted()Tests whether the current thread has been interrupted
isAlive()Tests if this thread is alive
isDaemon()Tests if this thread is a daemon thread
isInterrupted()Tests whether this thread has been interrupted
join()Waits for this thread to die
join(long millis)Waits at most millis milliseconds for this thread to die
run()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns
setContextClassLoader(ClassLoader cl)Sets the context ClassLoader for this Thread
setDaemon(boolean on)Marks this thread as either a daemon thread or a user thread
setDefaultUncaughtExceptionHandler( Thread.UncaughtExceptionHandler eh)Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread
setName(String name)Changes the name of this thread to be equal to the argument name.
setUncaughtExceptionHandler( Thread.UncaughtExceptionHandler eh)Set the handler invoked when this thread abruptly terminates due to an uncaught exception
setPriority(int newPriority)Changes the priority of this thread
sleep(long millis)Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers
start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread
toString()Returns a string representation of this thread, including the thread's name, priority, and thread group
yield()A hint to the scheduler that the current thread is willing to yield its current use of a processor

Methods Inherited from java. lang.Object Class 

  • equals()
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.