Méthode Java Object wait(long timeout)

Java String类Classe Object de Java


La méthode wait(long timeout) de Object place le thread courant dans un état d'attente (bloqué) jusqu'à ce qu'un autre thread appelle la méthodenotify() de cet objetouméthode notifyAll()ou jusqu'à ce que le délai d'attente défini par le paramètre timeout soit dépassé.

Si le paramètre timeout vaut 0, il n'y aura pas de délai d'attente, et il continuera d'attendre, comme pourla méthode wait()。

Le thread courant doit être le propriétaire du moniteur de cet objet ; sinon, une exception sera quand même levée :IllegalMonitorStateException.

Si le thread courant est interrompu par n'importe quel thread avant ou pendant l'attente, il lanceraInterruptedExceptionune exception.

Si le paramètre passé est illégal, il lèveraIllegalArgumentExceptionune exception.

Syntaxe

public final void wait(long timeout)

Parameters

  • timeout- temps d'attente (en millisecondes).

Valeur de retour

Aucune valeur de retour.

Exemple

L'exemple suivant démontre l'utilisation de la méthode wait(long timeout) :

Exemple

import java.util.Collections; import java.util.LinkedList; import java.util.List; public class ExampleTest extends Object { private List synchedList; public ExampleTest() { //Créer une liste synchronisée synchedList = Collections.synchronizedList(new LinkedList()); } //Supprimer un élément de la liste public String removeElement() throws InterruptedException { synchronized (synchedList) { //Attendre si la liste est vide while (synchedList.isEmpty()) { System.out.println("La liste est vide..."); synchedList.wait(10000); //Définir le temps d'attente System.out.println("En attente..."); } String element = (String) synchedList.remove(0); return element; } } //Ajouter un élément à la liste public void addElement(String element) { System.out.println("Ouverture..."); synchronized (synchedList) { //Ajouter un élément et notifier que l'élément est présent synchedList.add(element); System.out.println("Nouvel élément:'" + element + "'"); synchedList.notifyAll(); System.out.println("notifyAll appelé!"); } System.out.println("Fermeture..."); } public static void main(String[] args) { final ExampleTest demo = new ExampleTest(); Runnable runA = new Runnable() { public void run() { try { String item = demo.removeElement(); System.out.println("" + item); } catch (InterruptedException ix) { System.out.println("Exception interrompue !"); } catch (Exception x) { System.out.println("Exception levée."); } } }; Runnable runB = new Runnable() { //Effectuez l'opération d'ajout d'élément et démarrez la boucle public void run() { demo.addElement("Bonjour !"); } }; try { Thread threadA1 = new Thread(runA, "Google"); threadA1.start(); Thread.sleep(500); Thread threadA2 = new Thread(runA, "Exemple"); threadA2.start(); Thread.sleep(500); Thread threadB = new Thread(runB, "Taobao"); threadB.start(); Thread.sleep(1000); threadA1.interrupt(); threadA2.interrupt(); } catch (InterruptedException x) { } } }

La sortie du programme ci-dessus est :

List is empty...
List is empty...
Opening...
New Element:'Hello!'
notifyAll called!
Closing...
Waiting...
Waiting...
List is empty...
Hello!
Interrupted Exception!

Java String类Classe Object de Java

Autres extensions