Java 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:
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:
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.
OUTPUT:
OUTPUT:
Below is the thread life cycle:
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.
OUTPUT:
Note: set thread as daemon before starting a thread else it will throw below error.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved