在Java中,線程同步與互斥是非常重要的概念。以往我們使用synchronized關鍵字來鎖定某個方法或某段代碼塊,以保持線程的同步與互斥。在這篇文章中,我們將介紹另一個重要的關鍵字——notify()。notify()方法的作用是喚醒等待線程隊列中的某個線程,並將該線程移動到鎖池中。對於正在運行的線程,notify()方法不會產生任何影響。如果你想在某個時刻喚醒一個等待線程,你需要在代碼中仔細地實現它。
一、notify()方法的使用
我們將具體了解notify()方法的使用。notify()方法要在synchronized塊中調用,以確保該對象的鎖定機制。
public class NotifyTest {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0; i<100 ; i++) {
total += i;
}
notify();
}
}
}
在這個例子中,我們創建了兩個線程:一個線程是主線程,另一個線程是ThreadB。main線程在運行時,調用synchronized方法鎖定ThreadB對象。然後,main線程調用ThreadB.wait()方法來等待ThreadB線程的完成。在ThreadB線程完成之後,total的值會被列印出來。
二、notify()方法的限制
notify()方法除了喚醒等待線程之外,它並沒有其他的效果。如果一個線程被notify()喚醒,它不會立即執行。相反,該線程需要重新進入該對象的鎖池並等待重新獲得該對象的鎖,然後才能繼續執行。
還有一個限制是,notify()方法只會喚醒等待隊列中的其中一個線程,並且我們不能指定哪個線程會被喚醒。也就是說,我們無法控制具體喚醒哪一個等待線程。
三、notifyAll()方法的使用
我們介紹了notify()方法的基本使用和限制,現在我們將介紹另一個方法:notifyAll()。notifyAll()方法喚醒等待隊列中的所有線程。
public class NotifyAllTest {
public static void main(String[] args) {
Message msg = new Message("process it");
Waiter waiter1 = new Waiter(msg);
new Thread(waiter1,"waiter1").start();
Waiter waiter2 = new Waiter(msg);
new Thread(waiter2, "waiter2").start();
Notifier notifier = new Notifier(msg);
new Thread(notifier, "notifier").start();
System.out.println("All threads started");
}
}
class Message {
private String msg;
public Message(String str){
this.msg=str;
}
public String getMsg() {
return msg;
}
public void setMsg(String str) {
this.msg=str;
}
}
class Waiter implements Runnable{
private Message msg;
public Waiter(Message m){
this.msg=m;
}
public void run() {
String name = Thread.currentThread().getName();
synchronized (msg) {
try{
System.out.println(name+" waiting to get notified at time:"+System.currentTimeMillis());
msg.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(name+" waiter thread got notified at time:"+System.currentTimeMillis());
//process the message now
System.out.println(name+" processed: "+msg.getMsg());
}
}
}
class Notifier implements Runnable {
private Message msg;
public Notifier(Message msg) {
this.msg = msg;
}
public void run() {
Thread.currentThread().setName("notifier");
System.out.println("Started notifying at time:"+System.currentTimeMillis());
synchronized (msg) {
msg.setMsg("Notifying all waiting threads");
msg.notifyAll();
// msg.notify();
}
}
}
在這個例子中,我們創建了一個Message類,一個Waiter類和一個Notifier類。Waiter類等待Notifier類的通知,Notifier類發出通知,等待的Waiter類會被喚醒並繼續執行。
在這個例子中,我們使用了wait()和notifyAll()方法。wait()方法可將當前線程放入對象的等待隊列中,並釋放對象鎖,以便其他線程可以獲得該對象的鎖並繼續執行。notifyAll()方法將喚醒所有正在等待該對象的線程,這些線程將重新進入對象的鎖池,並準備重新獲得對象的鎖。
四、總結
在本篇文章中,我們介紹了Java中notify()方法的基本使用和限制,以及notifyAll()方法的使用。notify()方法是喚醒等待隊列中的某個線程,notifyAll()方法是喚醒等待隊列中的所有線程。在使用notify()和notifyAll()方法時,需要確保已經獲得對象鎖,以便它可以執行正確的操作。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193898.html
微信掃一掃
支付寶掃一掃