What Is Monitor in Java

Mohammad Irfan Oct 12, 2023
  1. Multithreading Example Without Using Monitor in Java
  2. Multithreading Example Using Monitor in Java
What Is Monitor in Java

This tutorial introduces what a monitor is and how to use it in Java programming.

Monitor is a term that refers to process synchronization. This is initially used by the Operating Systems, and now most of the programming languages use this.

In Java, it is used to achieve process synchronization in a multithreading environment. It helps to achieve mutual exclusion between processes. The outer process cannot access the code written in the monitor, so no wait and hold situation occurs.

Java uses the synchronized() method to create a monitor, and any object can serve as a monitor. A monitor uses a function associated with a specific data item/variable as a lock. When a thread tries to access and modify that data, then monitor restricts that process and holds until the current thread completes its execution.

Let’s understand the monitor with some examples.

Multithreading Example Without Using Monitor in Java

Let’s first understand what happens if we don’t use the monitor in multithreading programming. In this example, we created two threads and executed them. We can notice that the execution of threads is completely random, and anytime thread1 or thread2 start execution. In the monitor case, only one thread can execute its code at a time, and the second thread should wait for that time, but here we did not use monitor, so the result is completely messy. See the example below.

class MonitorDemo {
  void showMsg(String msg) { // synchronized method
    for (int i = 1; i <= 5; i++) {
      System.out.println(msg);
      try {
        Thread.sleep(500);
      } catch (Exception e) {
        System.out.println(e);
      }
    }
  }
}
class Thread1 extends Thread {
  MonitorDemo m;
  Thread1(MonitorDemo m) {
    this.m = m;
  }
  public void run() {
    m.showMsg("thread1");
  }
}
class Thread2 extends Thread {
  MonitorDemo m;
  Thread2(MonitorDemo m) {
    this.m = m;
  }
  public void run() {
    m.showMsg("thread2");
  }
}
public class SimpleTesting {
  public static void main(String args[]) {
    MonitorDemo obj = new MonitorDemo();
    Thread1 t1 = new Thread1(obj);
    Thread2 t2 = new Thread2(obj);
    t1.start();
    t2.start();
  }
}

Output:

thread1
thread2
thread1
thread2
thread1
thread2
thread1
thread2
thread1
thread2

Multithreading Example Using Monitor in Java

Here, we created a monitor in this example, and we marked the showMsg() method as synchronized. So, only one thread can access this at a time. You will notice the output this time; the second thread starts execution only after completing the first thread that makes the code synchronized, and this is the benefit of the monitor. It is helpful in multithreading programming when multiple threads request the same resource. We need to create such a system to avoid any deadlock or starvation problem. See the example below.

class MonitorDemo {
  synchronized void showMsg(String msg) { // synchronized method
    for (int i = 1; i <= 5; i++) {
      System.out.println(msg);
      try {
        Thread.sleep(500);
      } catch (Exception e) {
        System.out.println(e);
      }
    }
  }
}
class Thread1 extends Thread {
  MonitorDemo m;
  Thread1(MonitorDemo m) {
    this.m = m;
  }
  public void run() {
    m.showMsg("thread1");
  }
}
class Thread2 extends Thread {
  MonitorDemo m;
  Thread2(MonitorDemo m) {
    this.m = m;
  }
  public void run() {
    m.showMsg("thread2");
  }
}
public class SimpleTesting {
  public static void main(String args[]) {
    MonitorDemo obj = new MonitorDemo();
    Thread1 t1 = new Thread1(obj);
    Thread2 t2 = new Thread2(obj);
    t1.start();
    t2.start();
  }
}

Output:

thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2