Multithreading
Multithreading allows us to run two or more program consecutively. Each multiple program running consecutively called as thread.
Thread can be implemented by two ways in Java:
- By extending a Thread class.
- By implementing Runnable interface
Multitasking
In a layman’s term perform multiple tasks at the same time is called as multitasking and utilizes the CPU. It can be achieved in two ways:
- Process based multitasking (multiprocessing).
- Thread base multitasking (multithreading).
We can have a look on below diagram for better understanding on multiprocessing and multithreading. Operating system’s CPU runs multiple process at the same time as mentioned below and each process is running multiple thread along with it. Process p1 is running three threads as t1, t2 and t3, Process p1 is running two threads as t1 and t2. Process p3 is running only one thread as t1.
Thread creation:
By extends thread class
OUTPUT:
By implements Runnable interface
OUTPUT:
Thread Life cycle:
Below is the thread life cycle:
- New thread: It is created by extends class to a Thread class or implements Runnable method. It is not yet started, and its code didn’t run.
- Runnable: Thread comes in runnable state after calling start() method. Calling start() method doesn’t mean the thread will start running. The scheduler will decide the correct time to start and provide resource.
- Running: Scheduler will decide when to move thread from runnable to running state after calling start() method.
- Waiting (or Blocked): In this state thread is alive but will not be actively running. Thread goes in to waiting state after calling sleep) method or blocked state because of waiting for some other resource.
- Terminate: Terminate state will come once the execution is done for the block of code written inside run() method.
Thread Methods:
Daemon Thread:
Daemon thread is a low priority thread. It runs in the background to perform task such as garbage collection thread. Daemon thread gets terminated once all the user defined thread gets terminate. JVM don’t look for the daemon thread until the execution of user defined thread didn’t get complete.
Note: JVM will wait for user defined thread and will not be waiting for daemon thread to complete before exiting.
setDaemon() : This is the method which set thread as a daemon thred.
getDaemon() : This will return true if the thread is daemon else will written false.
Note: set thread as daemon before starting a thread else it will throw below error.
Daemon thread creation example:
OUTPUT:
Note: set thread as daemon before starting a thread else it will throw below error.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.