isDaemon isAlive isInterrupted getThreadGroup

isDaemon isAlive isInterrupted getThreadGroup

Summary: So far, we studied the methods like sleep(), start() and currentThread() etc. of Thread class. Let us study some more, very less used by the Programmers, like methods isDaemon isAlive isInterrupted getThreadGroup etc.

Program with Methods isDaemon isAlive isInterrupted getThreadGroup :
public class MoreMethods
{
  public static void main(String args[])
  {
    Thread t1 = new Thread();  
    System.out.println("t1 name by default: " + t1.getName());                // given Thread-0
    t1.setName("tax");          
    System.out.println("t1 name after a name is given: " + t1.getName());     // gives tax

    System.out.println("t1 priority by default: " + t1.getPriority());        // prints 5
    System.out.println("Thread group t1 default:" + t1.getThreadGroup());     // pints main

    t1.setPriority(Thread.NORM_PRIORITY+3);  
    System.out.println("t1 priority after setting: " + t1.getPriority());     // 8
  
    System.out.println("t1 is daemon by default: " + t1.isDaemon());          // false
    t1.setDaemon(true);     
    System.out.println("After making daemon, t1 is daemon: " + t1.isDaemon());// true

    System.out.println("t1 is alive: " + t1.isAlive());                       // false
    System.out.println("t1 is interrupted: " + t1.isInterrupted());           // false
  }
}

isDaemon isAlive isInterrupted getThreadGroup

A thread by default, at the time of birth (creation), comes with three properties which can be changed by the programmer if needed.

  1. Name:

    Every thread created gets by default a name. The first thread created gets Thread-0, the next one as Thread-1 and so on. With setName() method, a thread can be given a name and with getName() method, a thread's name can be retrieved.

  2. Priority:

    Every thread created gets by default a priority of 5, known as NORM_PRIORITY. A thread can be given a priority with setPriority() method and a thread's priority can be retrieved with getPriority() method.

  3. ThreadGroup:

    Every thread created should belong to some thread group. A thread group should be assigned to a thread at the time of creating the thread itself. If the programmer does not mention any group, the thread is placed in the default main group. Once a thread group is assigned, it cannot be reassigned to another group later. ThreadGroup is a class from java.lang package.

Now let us discuss the methods isDaemon isAlive isInterrupted getThreadGroup :

isAlive() method returns a boolean value. A programmer can create a number of threads of which a few may be active, some inactive and the other might have dead. To know the state of a thread, a programmer can use isAlive() method. This method returns true if the thread is in runnable state or blocked state. In born state and dead state, the method returns false.

isInterrupted() method returns true if the thread state (like execution or blocked time) is disturbed.

Daemon Threads

Daemon threads are service-oriented threads. A service-oriented thread, as the name indicates, serves all other threads. They die after every thread in the process dies, or to say, just before the process exits (no way related with the run() method execution). They are created before any thread is created and dies after every thread dies. For example, the JVM comes with a service thread, garbage collector which removes the garbage (unwanted objects etc.) generated while the process is going on.

The daemon threads are given lowest priority as they should come into action when no other thread is active as process execution (that gives output) must be of highest priority. A normal thread can be converted into a daemon thread with the following simple statement.

t1.setDaemon(true);

The boolean value true indicates t1 should be converted into a daemon thread. If false or if the statement is completely omitted, the thread is a non-daemon thread.

t1.isDaemon();

To test a thread is daemon or not, isDaemon() method is useful. It returns true if the thread is daemon, else false.

Inter thread communication

Communication between the threads can be done with join() method (between a parent and a child) and wait() and notify() methods (between a thread that locked the source and the other waiting for the lock).

I am sure you understood the Thread methods isDaemon isAlive isInterrupted getThreadGroup etc. elaborately. Pass your comments to increase the strength of the explanation.

1 thought on “isDaemon isAlive isInterrupted getThreadGroup”

Leave a Comment

Your email address will not be published.