Heim  >  Artikel  >  Java  >  Welche Bedeutung haben die Methoden wait(), notify() und notifyAll() in Java?

Welche Bedeutung haben die Methoden wait(), notify() und notifyAll() in Java?

WBOY
WBOYnach vorne
2023-09-04 09:57:061284Durchsuche

Welche Bedeutung haben die Methoden wait(), notify() und notifyAll() in Java?

Threads können über die Methoden wait(), notify() und notifyAll() in Java miteinander kommunizieren. Dies sind endgültige Methoden, die in der Object-Klasse definiert sind und nur aus einem synchronisierten Kontext aufgerufen werden können. Die Methode wait() bewirkt, dass der aktuelle Thread wartet, bis ein anderer Thread die Methode notify() oder notifyAll() des Objekts aufruft. Die notify()-Methode weckt einen einzelnen Thread , der auf den Monitor dieses Objekts wartet. Die Methode notifyAll() weckt alle Threads auf, die auf den Monitor dieses Objekts warten. Ein Thread wartet auf den Monitor eines Objekts, indem er eine der wait()-Methoden aufruft. Diese Methoden können eine IllegalMonitorStateException auslösen, wenn der aktuelle Thread nicht der Besitzer des Objektmonitors ist.

wait()-Methodensyntax

public final void wait() throws InterruptedException

notify()-Methodensyntax

public final void notify()

NotifyAll()-Methodensyntax

public final void notifyAll()<strong>
</strong>

Beispiel

public class WaitNotifyTest {
   private static final long SLEEP_INTERVAL<strong> </strong>= 3000;
   private boolean running = true;
   private Thread thread;
   public void start() {
      print("Inside start() method");
      thread = new Thread(new Runnable() {
         @Override
         public void run() {
            print("Inside run() method");
            try {
               Thread.sleep(SLEEP_INTERVAL);
            } catch(InterruptedException e) {
               Thread.currentThread().interrupt();
            }
            synchronized(WaitNotifyTest.this) {
               running = false;
               WaitNotifyTest.this.notify();
            }
         }
      });
      thread.start();
   }
   public void join() throws InterruptedException {
      print("Inside join() method");
      synchronized(this) {
         while(running) {
            print("Waiting for the peer thread to finish.");
            wait(); //waiting, not running
         }
         print("Peer thread finished.");
      }
   }
   private void print(String s) {
      System.out.println(s);
   }
   public static void main(String[] args) throws InterruptedException {
      WaitNotifyTest test = new WaitNotifyTest();
      test.start();
      test.join();
   }
}

Ausgabe

Inside start() method
Inside join() method
Waiting for the peer thread to finish.
Inside run() method
Peer thread finished.

Das obige ist der detaillierte Inhalt vonWelche Bedeutung haben die Methoden wait(), notify() und notifyAll() in Java?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen