In this article, we will discuss important methods and constructors of a Thread class
1. Thread :
- Thread is a separate flow of execution in a program/process
- Threads are light–weight processes or smallest unit of work in a process
- Creating a Thread or spawning a new Thread means allocating a separate independent flow of execution in a process
- So, we can create as many as Threads required for the process/program or in other way a process can contain multiple threads independent of each other
- Multiple threads of the same process share memory address of that process allowing inter–process communication faster and context–switching less expensive

1.1 Creating a Thread :
There are 2 ways to create a Thread in Java,
- By extending Thread class
- By implementing Runnable interface
In both the ways, run() method has to be implemented providing the logic/work that separate thread need to be executed
MyThread.java
public class MyThread extends Thread {
@Override
public void run() {
// work for new thread
}
}
MyRunnable.java
public class MyRunnable implements Runnable {
@Override
public void run() {
// work for new thread
}
}
2. Thread Constructors :
There are 8 constructors defined in Thread class, those are –
2.1 Thread t = new Thread();
- Allocates a new Thread object
ThreadConstructor1.java
package in.bench.resources.threads;
public class ThreadConstructor1 {
public static void main(String[] args) {
// create Thread - not started
Thread t = new Thread();
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= Thread-0
State= NEW
Thread Group= java.lang.ThreadGroup[name=main,maxpri=10]
Current executing thread name= main
2.2 Thread t = new Thread(Runnable r);
- Allocates a new Thread object
ThreadConstructor2.java
package in.bench.resources.threads;
public class ThreadConstructor2 {
public static void main(String[] args) {
// create instance of Runnable
MyRunnable r = new MyRunnable();
// create Thread passing Runnable instance - not started
Thread t = new Thread(r);
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= Thread-0
State= NEW
Thread Group= java.lang.ThreadGroup[name=main,maxpri=10]
Current executing thread name= main
2.3 Thread t = new Thread(String name);
- Allocates a new Thread object
ThreadConstructor3.java
package in.bench.resources.threads;
public class ThreadConstructor3 {
public static void main(String[] args) {
// create Thread - not started
Thread t = new Thread("bench3");
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= bench3
State= NEW
Thread Group= java.lang.ThreadGroup[name=main,maxpri=10]
Current executing thread name= main
2.4 Thread t = new Thread(Runnable r, String name);
- Allocates a new Thread object
ThreadConstructor4.java
package in.bench.resources.threads;
public class ThreadConstructor4 {
public static void main(String[] args) {
// create instance of Runnable
MyRunnable r = new MyRunnable();
// create Thread - not started
Thread t = new Thread(r, "bench4");
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= bench4
State= NEW
Thread Group= java.lang.ThreadGroup[name=main,maxpri=10]
Current executing thread name= main
2.5 Thread t = new Thread(ThreadGroup tg, String name);
- Allocates a new Thread object
ThreadConstructor5.java
package in.bench.resources.threads;
public class ThreadConstructor5 {
public static void main(String[] args) {
// create instance of ThreadGroup
ThreadGroup tg = new ThreadGroup("threadgroup5");
// create Thread - not started
Thread t = new Thread(tg, "thread5");
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= thread5
State= NEW
Thread Group= java.lang.ThreadGroup[name=threadgroup5,maxpri=10]
Current executing thread name= main
2.6 Thread t = new Thread(ThreadGroup tg, Runnable r);
- Allocates a new Thread object
ThreadConstructor6.java
package in.bench.resources.threads;
public class ThreadConstructor6 {
public static void main(String[] args) {
// create instance of Runnable
MyRunnable r = new MyRunnable();
// create instance of ThreadGroup
ThreadGroup tg = new ThreadGroup("threadgroup6");
// create Thread - not started
Thread t = new Thread(tg, r);
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= Thread-0
State= NEW
Thread Group= java.lang.ThreadGroup[name=threadgroup6,maxpri=10]
Current executing thread name= main
2.7 Thread t = new Thread(ThreadGroup tg, Runnable r, String name);
- Allocates a new Thread object so that –
- It has target as its run object
- Has the specified name as its name
- Belongs to the thread group referred to by group
ThreadConstructor7.java
package in.bench.resources.threads;
public class ThreadConstructor7 {
public static void main(String[] args) {
// create instance of Runnable
MyRunnable r = new MyRunnable();
// create instance of ThreadGroup
ThreadGroup tg = new ThreadGroup("threadgroup7");
// create Thread - not started
Thread t = new Thread(tg, r, "thread7");
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= thread7
State= NEW
Thread Group= java.lang.ThreadGroup[name=threadgroup7,maxpri=10]
Current executing thread name= main
2.8 Thread t = new Thread(ThreadGroup tg, Runnable r, String name, long stackSize);
- Allocates a new Thread object so that –
- It has target as its run object
- Has the specified
name
as its name - Belongs to the thread group referred to by
group
- Has the specified stack size
ThreadConstructor8.java
package in.bench.resources.threads;
public class ThreadConstructor8 {
public static void main(String[] args) {
// create instance of Runnable
MyRunnable r = new MyRunnable();
// create instance of ThreadGroup
ThreadGroup tg = new ThreadGroup("threadgroup8");
// create Thread - not started
Thread t = new Thread(tg, r, "thread8", 88);
// print below values
System.out.println("Id= " + t.getId());
System.out.println("Priority= " + t.getPriority());
System.out.println("Name= " + t.getName());
System.out.println("State= " + t.getState());
System.out.println("Thread Group= " + t.getThreadGroup());
System.out.print("Current executing thread name= " + Thread.currentThread().getName());
}
}
Output :
Id= 15
Priority= 5
Name= thread8
State= NEW
Thread Group= java.lang.ThreadGroup[name=threadgroup8,maxpri=10]
Current executing thread name= main
3. Thread Methods :
In this section, we will cover important methods of Thread class –
- getName()
- returns invoking thread‘s name
- setName(String name)
- Changes the name of the invoking thread with the supplied/passed argument name
- getPriority()
- returns invoking thread‘s priority
- setPriority(int newPriority)
- Changes the priority of the invoking thread
- getId()
- returns the identifier of the invoking thread
- getState()
- returns the state of the invoking thread
- getThreadGroup()
- returns the thread group to which the invoking thread belongs
- isDaemon()
- Checks whether the invoking thread is a daemon thread
- setDaemon(boolean on)
- Marks/sets the invoking thread as either a daemon thread or a user thread based on true/false value supplied/passed as method–argument
- currentThread()
- returns a reference to the currently executing thread object
- activeCount()
- returns an estimate of the number of active threads in the current thread‘s thread group and its sub-groups
- clone()
- Throws CloneNotSupportedException as a Thread can not be meaningfully cloned
- isInterrupted()
- Tests whether the invoking thread has been interrupted
- interrupted()
- Tests whether the current thread has been interrupted
- interrupt()
- Interrupts the invoking thread
- isAlive()
- Tests whether the invoking thread is alive
- run()
- If the invoking 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
- start()
- Causes the invoking thread to begin execution; the Java Virtual Machine (JVM) calls the run method of invoking thread
- join() – there are 3 variants of join() method
- join() – waits for the invoking thread to die
- join(long millis) – waits at most millis milliseconds for the invoking thread to die
- join(long millis, int nanos) – waits at most millis milliseconds plus nanos nanoseconds for the invoking thread to die
- sleep() – there are 2 variants of sleep() method
- 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
- sleep(long millis, int nanos) – causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers
- yield()
- A hint to the scheduler that the current thread is willing to yield its current use of a processor/CPU
- Below methods are deprecated –
- destroy()
- stop()
- resume()
- suspend()
- Below methods are inherited from class java.lang.Object –
- equals()
- finalize()
- getClass()
- hashCode()
- notify()
- notifyAll()
- wait()
We will see examples for few of the above mentioned methods
3.1 Setting & Getting Thread Names :
- setName(String) –
- This method changes/sets/modifies the thread name with passed/supplied value
- getName() –
- This method returns the thread name which could be default JVM provided name or explicitly set/modified by the programmer
ThreadNameDemo.java
package in.bench.resources.threads;
public class ThreadNameDemo {
public static void main(String[] args) {
// 1. create Thread - not started
Thread t = new Thread();
// 2. get current name of the name
String name = t.getName(); // default JVM provided
System.out.println("1. Current Thread Name = " + name);
// 3. set the name of the Thread
t.setName("Bench");
// 4. again, get the name of the Thread
name = t.getName();
System.out.print("\n2. After changing Thread Name = " + name);
}
}
Output :
1. Current Thread Name = Thread-0
2. After changing Thread Name = Bench
3.2 Setting & Getting Thread Priority :
- setPriority(int) –
- This method changes/sets/modifies the invoking thread priority with passed/supplied value
- getPriority() –
- This method returns the thread priority of the invoking Thread which could be default JVM provided priority or explicitly set by the programmer
ThreadPriorityDemo.java
package in.bench.resources.threads;
public class ThreadPriorityDemo {
public static void main(String[] args) {
// 1. create Thread - not started
Thread t = new Thread();
// 2. get thread priority
int priority = t.getPriority(); // default JVM provided
System.out.println("1. Current Thread Priority = " + priority);
// 3. set the priority of the Thread
t.setPriority(7);
// 4. again, get the priority of the Thread
priority = t.getPriority();
System.out.print("\n2. After changing Thread Priority = " + priority);
}
}
Output :
1. Current Thread Priority = 5
2. After changing Thread Priority = 7
3.3 Checking & Setting Daemon Thread :
- isDaemon() –
- Checks whether the invoking thread is a daemon thread
- setDaemon(boolean) –
- Marks/sets the invoking thread as either a daemon thread or a user thread based on true/false value passed as method–argument
DaemonThreadDemo.java
package in.bench.resources.threads;
public class DaemonThreadDemo {
public static void main(String[] args) {
// 1. create Thread - not started
Thread t = new Thread();
// 2. check Thread is daemon
boolean isDaemon = t.isDaemon(); // default JVM provided
System.out.println("1. Current Thread is daemon ? = " + isDaemon);
// 3. set the Thread to Daemon
t.setDaemon(true);
// 4. again, get the priority of the Thread
isDaemon = t.isDaemon();
System.out.print("\n2. After setting Thread to Daemon = " + isDaemon);
}
}
Output :
1. Current Thread is daemon ? = false
2. After setting Thread to Daemon = true
3.4 Check State of the Thread :
- getState() –
- returns the state of the invoking thread
- Different Thread State are –
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
ThreadStateDemo.java
package in.bench.resources.threads;
import java.lang.Thread.State;
public class ThreadStateDemo {
public static void main(String[] args) {
// 1. create Thread - not started
Thread t = new Thread();
// 2. check thread state
State state = t.getState(); // initial state provided by JVM
System.out.print("Current Thread State is = " + state);
}
}
Output :
Current Thread State is = NEW
3.5 Get current executing Thread :
- currentThread()
- returns a reference to the currently executing thread object
GetCurrentExecutingThread.java
package in.bench.resources.threads;
public class GetCurrentExecutingThread {
public static void main(String[] args) {
// 1. get current executing Thread
Thread currentThread = Thread.currentThread();
System.out.println("1. Current Thread details = " + currentThread);
// 2. get name of the current executing Thread
String currentThreadName = Thread.currentThread().getName();
System.out.print("\n2. Current Thread name is = " + currentThreadName);
}
}
Output :
1. Current Thread details = Thread[main,5,main]
2. Current Thread name is = main
3.6 Get Id & ThreadGroup of current executing Thread :
- getId()
- returns the identifier of the invoking Thread
- getThreadGroup()
- returns the thread group to which the invoking thread belongs
GetIdAndStateOfCurrentExecutingThread.java
package in.bench.resources.threads;
public class GetIdAndStateOfCurrentExecutingThread {
public static void main(String[] args) {
// 1. get current executing Thread
Thread currentThread = Thread.currentThread();
System.out.println("1. Current Thread details = " + currentThread);
// 2. get Id of the current executing Thread
long currentThreadId = Thread.currentThread().getId();
System.out.println("\n2. Id of Current Thread is = " + currentThreadId);
// 3. get Id of the current executing Thread
String currentThreadGroup = Thread.currentThread().getThreadGroup().getName();
System.out.print("\n3. Current Thread Group is = " + currentThreadGroup);
}
}
Output :
1. Current Thread details = Thread[main,5,main]
2. Id of Current Thread is = 1
3. Current Thread Group is = main
3.7 Interrupting a Thread :
- isInterrupted()
- Tests whether the invoking thread has been interrupted
- interrupted()
- Tests whether the current thread has been interrupted
- interrupt()
- Interrupts the invoking thread
ThreadInterruptionDemo.java
package in.bench.resources.threads;
public class ThreadInterruptionDemo {
public static void main(String[] args) {
// 1. create Thread - not started
Thread t = new Thread();
// 2. check whether Thread t is interrupted
boolean isInterrupted = t.isInterrupted();
System.out.println("1. Thread t is Interrupted ? = " + isInterrupted);
// 3. check current Thread is interrupted
boolean interrupted = Thread.currentThread().interrupted();
System.out.println("\n2. Current executing Thread is Interrupted ? = " + interrupted);
// 4. interrupt the Thread t
t.interrupt();
// 5. again, check whether Thread t is interrupted
isInterrupted = t.isInterrupted();
System.out.println("\n3. Again, Thread t is Interrupted ? = " + isInterrupted);
// 6. interrupt the current executing Thread
Thread.currentThread().interrupt();
// 7. check current Thread is interrupted
interrupted = Thread.currentThread().interrupted();
System.out.print("\n4. Current executing Thread is Interrupted ? = " + interrupted);
}
}
Output :
1. Thread t is Interrupted ? = false
2. Current executing Thread is Interrupted ? = false
3. Again, Thread t is Interrupted ? = true
4. Current executing Thread is Interrupted ? = true
Related Articles :
- Java – Introduction to Threads
- Java – Different ways to create/spawn a Thread
- Java – Thread Scheduler
- Java – Important methods and constructors of a Thread class
- Java – Difference between start() and run() methods of Thread class
- Java – Importance of start() method in Thread class
- Java – Overloading run() method of Thread class
- Java – Effects of not overriding run() method of Thread class
- Java – Setting and getting name of a Thread
- Java – Thread priorities
References :
- https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
Happy Coding !!
Happy Learning !!