Thread Methods Java

General thread methods in Java, the Programmer mostly uses are sleep(), getName(), setName(), setPriority(), join() etc. Thread class comes with more methods like isAlive(), activeCount(), setDaemon() etc. These methods are discussed herewith.

public class ThreadMethods
{
  public static void main(String args[])
  {                                                               //  THREAD NAME METHODS
    Thread newThread = new Thread();  
    System.out.println("newThread default name: " + newThread.getName());                  // prints Thread-0
    newThread.setName("shares");          
    System.out.println("newThread name after setting a name: " + newThread.getName());     // prints shares

                                                                  // THREAD PRIORITY METHODS
    System.out.println("newThread default priority: " + newThread.getPriority());          // prints 5
    newThread.setPriority(Thread.MAX_PRIORITY);  
    System.out.println("newThread priority after setting: " + newThread.getPriority());    // prints 10

                                                                  // THREAD NATURE METHODS
    System.out.println("Is newThread daemon by default: " + newThread.isDaemon());         // prints false
    newThread.setDaemon(true);   
    System.out.println("Is newThread Daemon after setting as true: " + newThread.isDaemon()); // prints true
    System.out.println("Is newThread alive: " + newThread.isAlive());                      // prints false
    System.out.println("Is newThread interrupted: " + newThread.isInterrupted());          // prints false

                                                                  // THREAD GROUP METHODS
    System.out.println("Thread group for which newThread belongs: " + newThread.getThreadGroup());   // prints main
    ThreadGroup tg = new ThreadGroup("sensex");
    Thread myThread = new Thread(tg, "points");
    System.out.println("myThread Name: " + myThread.getName());                            // prints Thread-0
    System.out.println("myThread belongs to thread group: " + myThread.getThreadGroup());  // prints sensex
    System.out.println("Number of active threads in tg thread group: " + tg.activeCount());// prints 0
  }
}

ssOutput screenshot on Thread Methods

Every thread created by the Programmer will have three properties with default values.

  1. Name: A thread should have a name. If not given by the Programmer, takes a default value of Thread-0 and the next one created comes with Thread-1 and so on.
  2. Priority: Every thread should born with a priority. If not set, it takes a default value of 5, known as Normal Priority.
  3. Thread Group: Every thread should belong to a thread group. If not given, it takes default value of main group.

System.out.println(“newThread default name: ” + newThread.getName()); // prints Thread-0
newThread.setName(“shares”);
System.out.println(“newThread name after setting a name: ” + newThread.getName()); // prints shares

With setName() method, a thread name can be given. In the code it is given as shares. With getName(), the thread name can be retrieved.

System.out.println(“newThread default priority: ” + newThread.getPriority()); // prints 5
newThread.setPriority(Thread.MAX_PRIORITY);
System.out.println(“newThread priority after setting: ” + newThread.getPriority()); // prints 10

setPriority() and getPriority() methods are useful to get and set the priority to threads. Priority setting involves special attention and is given in detail at Thread Priorities.

System.out.println(“Is newThread daemon by default: ” + newThread.isDaemon()); // prints false
newThread.setDaemon(true);
System.out.println(“Is newThread Daemon after setting as true: ” + newThread.isDaemon()); // prints true
System.out.println(“Is newThread alive: ” + newThread.isAlive()); // prints false
System.out.println(“Is newThread interrupted: ” + newThread.isInterrupted()); // prints false

Daemon Thread: Daemon threads are service threads which serve other threads. A good example is JVM’s garbage collector. Daemon threads are created before any thread created and destroyed after every thread dies. In realtime good example is Office Boy. He comes before any employee comes and leaves after everyone leaves. These service threads are given less priority. say minimum 1, because they should come into action when no other thread is active.

isAlive(): A thread is said to be live if it is in runnable state or blocked state. States are given in detail in Thread Life cycle . isAlive() is more discussed in Thread isAlive Java Example.

System.out.println(“Thread group for which newThread belongs: ” + newThread.getThreadGroup()); // prints main
ThreadGroup tg = new ThreadGroup(“sensex”);
Thread myThread = new Thread(tg, “points”);
System.out.println(“myThread Name: ” + myThread.getName()); // prints Thread-0
System.out.println(“myThread belongs to thread group: ” + myThread.getThreadGroup()); // prints sensex

Every thread should belong to a thread group and the default is main group. Do not confuse, there is a thread group called main. Any group you create will become as subgroup of main group. This is how JVM organizes groups. Once a group is set, it cannot be changed throughout life cycle and more over the group should be given at the time thread creation itself, if required as in the above code.

System.out.println(“Number of active threads in tg thread group: ” + tg.activeCount());// prints 0

A Programmer can know how many threads are active in a thread group with activeCount() method. In the code it printed 0 as the threads are created but not started with start() method.

Leave a Comment

Your email address will not be published.