Thread Life Cycle and Constructors in Multithreading- Java

Thread Life Cycle: In java applications, Threads are able to have the following states as part of their life-cycle.
1. New/Born State: When we create Thread class object in java applications then Thread will come to New/Born state.
2.Ready/Runnable State: When we access start() method Thread Schedular has to assign system resources like memory and time, here before assigninig system resources and after calling start() method is called as Ready/Runnable state.
3.Running State: In java applications, after calling start() method and after getting system resources
like memory and execution time is called as "Running State".
NOTE: We can send a thread from Running state to Runnable state directly by accessing yield() method , but, it is not supported by Windows operating system, because, it will perform its functionality on the basis of Threads priority values, priority based operations are not supported by windows operating system.
4.Dead/Destroy State: In java applications, when we access stop() method over Running thread then that thread will come to Dead/Destroy state.
5.Blocked State: In java applications, we are able to keep a thread in Blocked state from Running state in the following situations.
a)When we access sleep(--) method with a particular sleep time.
b)When we access wait() method.
c)When we access suspend() method.
d)When we perform IO Operations.

In java applications, we are able to bring a thread from Blocked state to Ready/Runnable state in the following situations.
a)When sleep time is over.
b)If any other thread access notify() / notifyAll() methods.
c)If any other thread access resume() method.
d)When IO Operations are completed.

(img soure is taken from w3school.com)

Consrtuctors:

1. Thread(): Allocates a new Thread object (Thread t= new thread();)
2. Thread(Runnable target): Allocates a new Thread object (Thread t = new Thread(Runnable r))
3. Thread(String name): Allocates a new Thread object (Thread t = new Thread(String name))
4. Thread(Runnable target, String name): Allocates a new Thread object (Thread t = new Thread(Runnable target, String name))
5. Thread(ThreadGroup group, String name): Allocates a new Thread object
6. Thread(ThreadGroup group, Runnable target): Allocates a new Thread object
7. Thread(ThreadGroup group, Runnable target, String name): Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group
8. Thread(ThreadGroup group, Runnable target, 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, and belongs to the thread group referred to by group, and has the specified stack size

Examples of constructors

1. public Thread()
--> This constructor can be used to create thread class object with the following properties.
       Thread Name: Thread-0
       Thread Priority: 5
       Thread group Name : main
EX: Thread t=new Thread();
       System.out.println(t);
OP: Thread[Thread-0, 5, main]
2. public Thread(String name)

-->This constructor can be used to create Thread class object with the specified name.

EX: Thread t=new Thread("Core Java");
       System.out.println(t);
OP: Thread[Core Java,5,main]
3. public Thread(Runnable r)
-->This constructor can be used to create Thread class object with the specified Runnable reference.
EX: Runnable r=new Thread();
 Thread t=new Thread(r);
 System.out.println(t);
OP: Thread[Thread-1,5,main]

4. public Thread(Runnable r, String name)
  -->This constructor can be used to create Thread class object with the specified Runnable reference 
and with the specified name.
EX: Runnable r=new Thread();
       Thread t=new Thread(r, "Core Java");
        System.out.println(t);
OP: Thread[Core Java,5,main]
5. public Thread(Runnable r)
-->This constructor can be used to create Thread class object with the specified Runnable reference.
EX: Runnable r=new Thread();
 Thread t=new Thread(r);
 System.out.println(t);
OP: Thread[Thread-1,5,main]
6.public Thread(ThreadGroup tg, String name)
-->This constructor can be used to create Thread class object with the specified ThreadGroup name and with the specified Thread name.
EX: ThreadGroup tg=new ThreadGroup("Java");
Thread t=new Thread(tg, "Core Java");
System.out.println(t);
OP:Thread[Core Java,5,Java]
7. public Thread(ThreadGroup tg, Runnable r, String name)
--> This constuctor constuctor can be used to create Thread class object with the specified ThreadGroup name, with the Runnable reference and with the thread name.

EX: ThreadGroup tg=new ThreadGroup("Java");
 Runnable r=new Thread();
 Thread t=new Thread(tg, r, "Core Java");
 
 OP: Thread[Core Java, 5, Java]

Program:

class MultithreadingDemo extends Thread
{
public void run()
{
  System.out.println(Thread.currentThread().getName());
}
MultithreadingDemo(String name) 
 {
   super(name);
 }
}
public class Demo1
 {
   public static void main(String[] args)
 {
   MultithreadingDemo mt = new MultithreadingDemo("sookshmas");
    mt.start();
   System.out.println("Main");
  }
}
class MultithreadingDemo implements Runnable
{
public void run()
{
    System.out.println(Thread.currentThread().getName());
}
}
public class Demo1 {
     public static void main(String[] args)
 {
       MultithreadingDemo mt = new MultithreadingDemo();
      Thread t= new Thread(mt,"Sookshmas");
        t.start();
     System.out.println("Main Thread");
 }
}

Note: Every Thread has name :- It can be provided by JVM externally given by user.

Below program to check with thread name by jvm and by user:

class MyThread extends Thread
{
}
public class Demo1 {

     public static void main(String[] args) 
  {
     System.out.println(Thread.currentThread().getName()); //assigned by jvm
      MyThread t = new MyThread();
         System.out.println(t.getName());
    Thread.currentThread().setName("Sookshmas"); //user setting the thread name
    System.out.println(Thread.currentThread().getName());
  }
}

Posted on by