Java-Thread Class Methods

Thread Class Methods

The Thread class possesses many useful static and instance methods. The important methods that are available in the Thread class are briefly described in the following table:

Method Purpose
1) static Thread currentThread() To return a reference to the current thread.
2) static void sleep(long m) To cause the current thread to wait for m milliseconds.
3) void start() To start the specific thread.
4) void run() This method encapsulates the functionality of a thread. This method is always overridden.
5) boolean isAlive() To return the value true if a thread has been started and has not yet died. Otherwise, it returns the value false.
6) void setName(String str) To set the name of the specified thread to str.
7) String getName() To return the name of the specified thread.
8) void setPriority(int p) To set the priority of the specified thread to p.
9) int getPriority() To return the priority of the specified thread.

Getting Thread Name

You can obtain the name assigned to a thread by calling the getName( ) method for the Thread object. The name of the thread is returned as a default Thread name (Thread-0,Thread-1, Thread-2,…………).



Program Source

class NewThread extends Thread {
    void changeName(String str)
    public void run()
        System.out.println("Exiting : "+getName());

public class Javaapp {
    public static void main(String[] args) {
        NewThread th1 = new NewThread();
        NewThread th2 = new NewThread();
        NewThread th3 = new NewThread();

Leave a Comment