Java 中的 Monitor 是什麼

Mohammad Irfan 2023年1月30日 2022年5月11日
  1. 在 Java 中不使用 Monitor 的多執行緒示例
  2. 在 Java 中使用 Monitor 的多執行緒示例
Java 中的 Monitor 是什麼

本教程介紹了 Monitor 是什麼以及如何在 Java 程式設計中使用它。

Monitor 是一個術語,指的是程序同步。這最初由作業系統使用,現在大多數程式語言都使用它。

在 Java 中,用於在多執行緒環境中實現程序同步。它有助於實現程序之間的互斥。外部程序無法訪問 Monitor 中編寫的程式碼,因此不會發生等待和保持情況。

Java 使用 synchronized() 方法來建立 Monitor,任何物件都可以用作 Monitor。Monitor 使用與特定資料項/變數關聯的函式作為鎖。當執行緒嘗試訪問和修改該資料時,Monitor 會限制該程序並保持直到當前執行緒完成其執行。

讓我們通過一些例子來理解 Monitor。

在 Java 中不使用 Monitor 的多執行緒示例

我們先來了解一下,如果我們在多執行緒程式設計中不使用 Monitor 會發生什麼。在這個例子中,我們建立了兩個執行緒並執行了它們。我們可以注意到執行緒的執行是完全隨機的,執行緒 1 或執行緒 2 隨時開始執行。在 monitor 的情況下,一次只有一個執行緒可以執行它的程式碼,第二個執行緒應該等待那個時間,但是這裡我們沒有使用 monitor,所以結果完全混亂。請參閱下面的示例。

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();  
	}  
}  

輸出:

thread1
thread2
thread1
thread2
thread1
thread2
thread1
thread2
thread1
thread2

在 Java 中使用 Monitor 的多執行緒示例

在這裡,我們在此示例中建立了一個 Monitor,並將 showMsg() 方法標記為同步。因此,一次只有一個執行緒可以訪問它。這次你會注意到輸出;第二個執行緒只有在完成第一個使程式碼同步的執行緒後才開始執行,這就是 Monitor 的好處。當多個執行緒請求相同的資源時,它有助於多執行緒程式設計。我們需要建立這樣一個系統來避免任何死鎖或飢餓問題。請參閱下面的示例。

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();  
	}  
}  

輸出:

thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2