Methods associated with threads in Java With Examples

There are several methods that are available for efficient utilization of thread to set general properties e.g priority, name. There are methods that give information regarding the state and also allow operations on the thread.

 Let us look at a few of them.

 start()

Declaration: void start()
Description:  Causes the calling thread to begin execution i.e Java Virtual Machine calls the run method of calling thread.

Example:

class MyThread extends Thread{
 MyThread(){
 super("My First Thread");
 }
public void run(){
System.out.println("I am inside run method of MyThread triggered by start");
  }
}

public class ClassMain{
 public static void main(String args[]){
 MyThread ob = new MyThread();
 System.out.println("The thread is "+ob);
 ob.start();
 }
}

Output:

The thread is Thread[My First Thread,5,main]
I am inside run method of MyThread triggered by start

activeCount ()

Declaration: static int activeCount()
Description:  It returns the total number of active thread in the current thread

Example:

class MyThread extends Thread{
    MyThread(){
        super("My First Thread");
    }
 public void run(){}
}

public class ClassMain{
    public static void main(String args[]){
        System.out.println("Active count of thread is "+Thread. activeCount());
        System.out.println("A new thread added now");
        MyThread ob = new MyThread();
        ob.start();
        System.out.println("Active count of thread is "+Thread. activeCount());
    }
}

Output:

Active count of thread is 1
A new thread added now
Active count of thread is 2

currentThread()

Declaration: static Thread  currentThread ()
Description:  It returns the reference of the current thread

Example:

class MyThread extends Thread{
    MyThread(){
        super("My First Thread");
    }  

    public void run(){
        System.out.println("The currently active thread is "+Thread.currentThread());
    }
}

public class ClassMain{
    public static void main(String args[]){
        System.out.println("The currently active thread is "+Thread.currentThread());
        MyThread ob = new MyThread();
        ob.start();
    }
}

Output:

The currently active thread is Thread[main,5,main]
The currently active thread is Thread[My First Thread,5,main]

getId()

Declaration: long getId()
Description: It returns the identifier of the current thread

Example:

class MyThread extends Thread{
    MyThread(){
        super("My First Thread");
    }
    public void run(){
        System.out.println("<Inside run>The id of currently active thread is "+this.getId());
    }
}
public class ClassMain{
    public static void main(String args[]){
        MyThread ob = new MyThread();
        ob.start();
        System.out.println("<Outside run inside main>The id of currently active thread is "+ob.getId());
    }
}

Output:

<Outside run inside main>The id of currently active thread is 21
<Inside run>The id of currently active thread is 21

getName()

Declaration: String getName()
Description: It returns the name of the current thread.

Example:

class MyThread extends Thread{
    MyThread(){
        super("My First Thread");
    }

    public void run(){
        System.out.println("<Inside run>The name of currently active thread is "+this.getName());
    }
}

public class ClassMain{
    public static void main(String args[]){
        MyThread ob = new MyThread();
        ob.start();
        System.out.println("<Outside run inside main>The name of currently active thread is "+ob.getName());
    }
}

Output:

<Outside run inside main>The name of currently active thread is My First Thread
<Inside run>The name of currently active thread is My First Thread

setName()

Declaration: void setName()
Description: It sets the name to the calling thread.

Example:

public class ClassMain extends Thread{
    
    ClassMain(String s){
        super(s);
    }
    
    public void run() {
   }
    public static void main(String args[]){
        ClassMain ob = new ClassMain("My_First_Thread");
        ob.start();
        System.out.println("<Before>The name of ob is "+ob.getName());
        ob.setName("My_New_Name");
        System.out.println("<After>The priority of ob is "+ob.getName());
    }
}

Output:

<Before>The name of ob is My_First_Thread
<After>The priority of ob is My_New_Name

getPriority()

Declaration: int getPriority()
Description: It returns the priority of the calling thread

Example:

class MyThread extends Thread{
    MyThread(String name){
        super(name);
    }
      public void run(){
    }
}

public class ClassMain{
    public static void main(String args[]){
        MyThread ob = new MyThread("My first thread");
        MyThread ob2 = new MyThread("My second thread");
        ob.start();
        ob2.start();
        System.out.println("The priority of ob is "+ob.getPriority());
        System.out.println("The priority of ob2 is "+ob.getPriority());
    }
}

 Output:

The priority of ob is 5
The priority of ob2 is 5

 setPriority()

Declaration: void setPriority()
Description: It sets the priority to the calling thread. This priority ranges from 1 to 10(1 being MIN_PRIORITY, 5 being NORM_PRIORITY,10 being MAX_PRIORITY

Example:

public class ClassMain extends Thread{
    public void run() {
   }
    public static void main(String args[]){
        ClassMain ob = new ClassMain();
        ob.start();
        System.out.println("<Before>The priority of ob is "+ob.getPriority());
        ob.setPriority(10);
        System.out.println("<After>The priority of ob is "+ob.getPriority());
    }
}

 Output:

<Before>The priority of ob is 5
<After>The priority of ob is 10

sleep()

Declaration: static void sleep(long millisec)
Description: It causes the currently executing thread to sleep for the mentioned(input) milliseconds

Example:

class MyThread extends Thread{
 MyThread(String s){
 super(s);
 }
public void run(){
    try{
      for(int i=0;i<5;i++){
      System.out.println("MyThread:"+i);
      Thread.sleep(500);  //MyThread sleeps for 500 miliseconds
       }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
  }
}

public class ClassMain{
 public static void main(String args[]){
 MyThread ob = new MyThread("First_Thread");
 ob.start();
  try{
      for(int i=0;i<5;i++){
      System.out.println("Main Thread:"+i);
      Thread.sleep(1000); //MainThread sleeps for 1000 miliseconds
       }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
 }
}

 Output:

Main Thread:0
MyThread:0
MyThread:1
Main Thread:1
MyThread:2
MyThread:3
Main Thread:2
MyThread:4
Main Thread:3
Main Thread:4

getState()

Declaration: void getState()
Description: It returns the state of the calling thread.

Example:

class MyThread extends Thread{
 MyThread(String s){
 super(s);
 }
public void run(){}
}

public class ClassMain{
 public static void main(String args[]){
 MyThread ob = new MyThread("First_Thread");
 try{
 System.out.println("State of MyThread:"+ob.getState()); //Outputs NEW
 ob.start();
  System.out.println("State of MyThread:"+ob.getState()); //Outputs RUNNABLE
 ob.sleep(2500);
 System.out.println("State of MyThread:"+ob.getState()); //Outputs TERMINATED
 }catch(InterruptedException ie){
    ie.printStackTrace();
    }
 }
}

 Output:

State of MyThread:NEW
State of MyThread:RUNNABLE
State of MyThread:TERMINATED

This was an overview of methods related to Thread. We will discuss Java I/O in the next lecture.