Maison > Java > javaDidacticiel > Quelle est l'importance des méthodes wait(), notify() et notifyAll() en Java ?

Quelle est l'importance des méthodes wait(), notify() et notifyAll() en Java ?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2023-09-04 09:57:06
avant
1402 Les gens l'ont consulté

Quelle est limportance des méthodes wait(), notify() et notifyAll() en Java ?

Les threads peuvent communiquer entre eux via les méthodes wait(), notify() et notifyAll() en Java. Ce sont des méthodes finales définies dans la classe Object et ne peuvent être appelées qu'à partir d'un contexte synchronisé . La méthode wait() fait attendre le thread actuel jusqu'à ce qu'un autre thread appelle la méthode notify() ou notifyAll() de l'objet. La méthode notify() réveille un seul thread qui attend le moniteur de cet objet. La méthode notifyAll() réveille tous les threads qui attendent le moniteur de cet objet. Un thread attend le moniteur d'un objet en appelant l'une des méthodes wait(). Ces méthodes peuvent lever une IllegalMonitorStateException si le thread actuel n'est pas le propriétaire du moniteur d'objets.

syntaxe de la méthode wait()

public final void wait() throws InterruptedException
Copier après la connexion

syntaxe de la méthode notify()

public final void notify()
Copier après la connexion

syntaxe de la méthode NotifyAll()

public final void notifyAll()<strong>
</strong>
Copier après la connexion

exemple

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();
   }
}
Copier après la connexion

sortie

Inside start() method
Inside join() method
Waiting for the peer thread to finish.
Inside run() method
Peer thread finished.
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal