Thread, Types and Life Cycle Of Thread

Thread, Types and Life Cycle Of Thread

Definition: Thread is a light weight process which helps in running the tasks in parallel.

Definition: Multi-thread program means more than one thread are going to run independently with optimal resources at a same time in a single program.
Main thread in Java
[Fig. Main thread in Java]
  • Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. 

Single thread vs. Multi thread:
  • Single threading: When the OS does not recognize the concept of thread
  • Multi-threading: When the OS supports multiple threads of execution within a single process 

  • Example:
    Single thread vs Multiple thread
    [Fig. Single thread Vs. Multiple thread per process]
  • Example: 
    • MS-DOS: support a single user process and a single thread
    • UNIX : supports multiple user processes but only supports one thread per process
    • Solaris : supports multiple threads
Application:
  • By definition, multitasking is when multiple processes share common processing resources such as a CPU. 
  • Example:
    [Fig: Multitasking by Employee in Office]
  • Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. 
  • Each of the threads can run in parallel. 
  • The OS divides processing time not only among different applications, but also among each thread within an application.
Why we used thread in Java:
  • It is used for making concurrent process or execution.
  • All java program must have at-least one thread is known as main thread.
  • A main thread is divided into small sub threads which are running parallel for faster processing within a same program.
  • The threads works independently and provides the maximum utilization of the CPU, thus enhancing the CPU performance.
  • Threads are use to perform asynchronous or background processing.
  • Threads to make Java application faster by doing multiple things at same time.
  • It Increases the responsiveness of GUI applications and take advantage of multiprocessor systems.
How thread works in Java:
  • When a Java program starts, the Java Virtual Machine creates the main thread and calls the program's main() method within that thread.
  • It is called as Main thread or Default thread of the Java program.
  • Example:
    Program: MyThread.java
    [Program: MyThread.java]
  • A main thread is automatically created when your program is started.
    • Every child thread created from main thread.
    • Program will terminated when main thread stop to execute.
    • Main thread can be controlled using the object of Thread Class.
  • Java is a language which can handle more than one thread at a same time is called as Multi-thread programming.
  • So, thread can be either daemon or non-daemon.
  • Example:
    types of thread
    [Fig. Types of thread]
    • Daemon thread:
      • The JVM will exit if only Daemon thread are executing.
      • Daemon thread are sometimes called "service" or "background" threads.
      • This kinds of thread that normally run at a low priority.
      • It is used to provide basic service to program.
        • Example: A garbage collector thread is running continuously.
      • To set any thread as daemon thread in Java. 
        • Example: threadObject.setDaemon(true)
    • Non-Daemon thread:
      • The JVM always has a main thread as default.
      • The JVM will not exit if non-Daemon thread are executing.
      • So, by default threads are non-daemon threads.
      • Daemon thread are die when JVM exits.
        • Example: main thread.
      • To check current thread nature using isDaemon() method of Thread class. 
Thread Priorities
  • Threads with higher priority or lower priority.
  • All higher priority are more important to a program.
  • It should be allocated processor time before lower-priority threads. 
  • Every Java thread has a priority.
  • Why Priorities given to thread?
    • Ans: It is helps the OS to determine the order in which threads are scheduled.
  • Java thread priorities are in the range between 1 to 10.
  • It cal also define with MIN_PRIORITY (as 1) and MAX_PRIORITY (as 10). 
  • By default, every thread is given priority to NORM_PRIORITY (as 5).
  • Example:
  • [Program- Mainthread.java]
    [Program: MainThread.java]
  • Output:
    • Note: Thread priorities cannot guarantee the order in which threads execute and are platform dependent.
Youtube video: Sarthak Edu

Life Cycle of thread:
  • Now, you have enough information, what is thread and multi-thread. 
  • Let us understand, how a single thread will works.
  • A thread has specific time span to finish its task and that time span is called as Life cycle of that thread.
  • Within a Life cycle of thread span, a thread has several phases or states.
  • During life cycle of thread, a thread may change one phase or state to another phase or state.
  • The java.lang.Thread class contains a static State enum – which defines its potential states or phases.
  • In Java, to get the current state of the thread, use Thread.getState() method to get the current state of the thread.
  • It has following values:
    • NEW: newly created thread that has not yet started the execution
      • Example: public static final Thread.State NEW
    • RUNNABLE: either running or ready for execution but it's waiting for resource allocation
      • Example: public static final Thread.State RUNNABLE
    • BLOCKED: waiting to acquire a monitor lock to enter or re-enter a synchronized block/method
      • Example: public static final Thread.State BLOCKED
    • WAITING: waiting for some other thread to perform a particular action without any time limit
      • Example: public static final Thread.State WAITING
    • TIMED_WAITING: waiting for some other thread to perform a specific action for a specified period
      • Example: public static final Thread.State TIMED_WAITING
    • TERMINATED: has completed its execution
      • Example: public static final Thread.State TERMINATED
  • Example:
    Life cycle of thread
    [Fig. Life cycle of thread]
Implementation of thread:
  • Every thread in Java is created and controlled by the java.lang.Thread class.
  • A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.
  • Java can be implemented using java.lang.Thread class and java.lang.Runnable Interface.
  • Difference between Thread vs. Runnable: 
    • Thread is class while Runnable is an interface.
    • The one advantage of using Runnable Interface over Thread class, is that the class which implements Runnable interface can extends one class or implement multiple interfaces which is not possible by extending Thread class.
    • Using applets, Thread class can not be extended. Therefore one has to implement the Runnable Interface.
  • Thread using Thread Class:
    • To declare a class that is sub-class of the class Thread defined in java.lang package.
extends Thread
[Extends Thread skeleton]

  • Thread using Runnable Class:
    • To declare a class that is implements by Runnable interface that defined in java.lang package.
implement Runnable skeleton
[Implement Runnable skeleton]

In this post, we learned about the life-cycle of a thread in Java with real time applications. We looked at all different states defined by Thread.State. Please, give your review, comment and suggestions for the above post.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form