Java Multithreading

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:

  1. By extending a Thread class.
  2. 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:

  1. Process based multitasking (multiprocessing).
  2. 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:

  1. 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.
  2. 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.
  3. Running: Scheduler will decide when to move thread from runnable to running state after calling start() method.
  4. 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.
  5. 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

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.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan