public final void wait() throws InterruptedException
public final void notify()



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()-Methodensyntaxpublic final void wait() throws InterruptedException
Nach dem Login kopieren
notify()-Methodensyntaxpublic final void wait() throws InterruptedException
public final void notify()
Nach dem Login kopieren
NotifyAll()-Methodensyntaxpublic final void notify()
public final void notifyAll()<strong>
</strong>
Nach dem Login kopieren
Beispielpublic final void notifyAll()<strong> </strong>
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();
}
}
Nach dem Login kopieren
Ausgabepublic 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(); } }
Inside start() method
Inside join() method
Waiting for the peer thread to finish.
Inside run() method
Peer thread finished.
Nach dem Login kopieren
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!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

