java thread interrupt,Java Thread等待,通知和notifyAll示例

 2023-11-19 阅读 28 评论 0

摘要:The Object class in java contains three final methods that allows threads to communicate about the lock status of a resource. These methods are wait(), notify() and notifyAll(). So today we will look into wait, notify and notifyAll in java program. Java中

The Object class in java contains three final methods that allows threads to communicate about the lock status of a resource. These methods are wait(), notify() and notifyAll(). So today we will look into wait, notify and notifyAll in java program.

Java中的Object類包含三個最終方法,這些方法允許線程就資源的鎖定狀態進行通信。 這些方法是wait()notify()notifyAll() 。 因此,今天我們將探討java程序中的wait,notify和notifyAll。

用Java等待,通知和notifyAll (wait, notify and notifyAll in Java)

The current thread which invokes these methods on any object should have the object monitor else it throws java.lang.IllegalMonitorStateException exception.

當前在任何對象上調用這些方法的線程都應具有對象監視器,否則它將引發java.lang.IllegalMonitorStateException異常。

等待 (wait)

Object wait methods has three variance, one which waits indefinitely for any other thread to call notify or notifyAll method on the object to wake up the current thread. Other two variances puts the current thread in wait for specific amount of time before they wake up.

java thread interrupt, 對象等待方法具有三種變化,一種無限期等待任何其他線程調用該對象上的notify或notifyAll方法來喚醒當前線程。 其他兩個差異會使當前線程在喚醒之前等待特定的時間。

通知 (notify)

notify method wakes up only one thread waiting on the object and that thread starts execution. So if there are multiple threads waiting for an object, this method will wake up only one of them. The choice of the thread to wake depends on the OS implementation of thread management.

notify方法僅喚醒一個正在等待該對象的線程,并且該線程開始執行。 因此,如果有多個線程在等待一個對象,則此方法將僅喚醒其中一個。 喚醒線程的選擇取決于線程管理的OS實現。

notifyAll (notifyAll)

notifyAll method wakes up all the threads waiting on the object, although which one will process first depends on the OS implementation.

notifyAll方法喚醒等待對象的所有線程,盡管首先處理哪個線程取決于OS的實現。

These methods can be used to implement producer consumer problem where consumer threads are waiting for the objects in Queue and producer threads put object in queue and notify the waiting threads.

Java thread, 這些方法可用于實現生產者消費者問題 ,其中消費者線程正在等待Queue中的對象,而生產者線程則將對象放入隊列中并通知正在等待的線程。

Let’s see an example where multiple threads work on the same object and we use wait, notify and notifyAll methods.

讓我們看一個示例,其中多個線程在同一個對象上工作,我們使用wait,notify和notifyAll方法。

信息 (Message)

A java bean class on which threads will work and call wait and notify methods.

線程將在其上工作的Java bean類,并調用wait和notify方法。

package com.journaldev.concurrency;public 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;}}

服務員 (Waiter)

A class that will wait for other threads to invoke notify methods to complete it’s processing. Notice that Waiter thread is owning monitor on Message object using synchronized block.

java wait和notify怎么用, 一個將等待其他線程調用notify方法以完成其處理的類。 注意,Waiter線程使用同步塊在Message對象上擁有監視器。

package com.journaldev.concurrency;public class Waiter implements Runnable{private Message msg;public Waiter(Message m){this.msg=m;}@Overridepublic 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 nowSystem.out.println(name+" processed: "+msg.getMsg());}}}

通知者 (Notifier)

A class that will process on Message object and then invoke notify method to wake up threads waiting for Message object. Notice that synchronized block is used to own the monitor of Message object.

一個將在Message對象上處理,然后調用notify方法以喚醒等待Message對象的線程的類。 注意,同步塊用于擁有Message對象的監視器。

package com.journaldev.concurrency;public class Notifier implements Runnable {private Message msg;public Notifier(Message msg) {this.msg = msg;}@Overridepublic void run() {String name = Thread.currentThread().getName();System.out.println(name+" started");try {Thread.sleep(1000);synchronized (msg) {msg.setMsg(name+" Notifier work done");msg.notify();// msg.notifyAll();}} catch (InterruptedException e) {e.printStackTrace();}}}

WaitNotifyTest (WaitNotifyTest)

Test class that will create multiple threads of Waiter and Notifier and start them.

測試類,該類將創建Waiter和Notifier的多個線程并啟動它們。

package com.journaldev.concurrency;public class WaitNotifyTest {public static void main(String[] args) {Message msg = new Message("process it");Waiter waiter = new Waiter(msg);new Thread(waiter,"waiter").start();Waiter waiter1 = new Waiter(msg);new Thread(waiter1, "waiter1").start();Notifier notifier = new Notifier(msg);new Thread(notifier, "notifier").start();System.out.println("All the threads are started");}}

When we will invoke the above program, we will see below output but program will not complete because there are two threads waiting on Message object and notify() method has wake up only one of them, the other thread is still waiting to get notified.

java scheduled注解。 當我們調用上面的程序時,我們將看到下面的輸出,但是程序不會完成,因為有兩個線程正在等待Message對象,而notify()方法僅喚醒了其中一個,另一個線程仍在等待得到通知。

waiter waiting to get notified at time:1356318734009
waiter1 waiting to get notified at time:1356318734010
All the threads are started
notifier started
waiter waiter thread got notified at time:1356318735011
waiter processed: notifier Notifier work done

If we comment the notify() call and uncomment the notifyAll() call in Notifier class, below will be the output produced.

如果我們在Notifier類中注釋notify()調用并取消注釋notifyAll()調用,則下面將是生成的輸出。

waiter waiting to get notified at time:1356318917118
waiter1 waiting to get notified at time:1356318917118
All the threads are started
notifier started
waiter1 waiter thread got notified at time:1356318918120
waiter1 processed: notifier Notifier work done
waiter waiter thread got notified at time:1356318918120
waiter processed: notifier Notifier work done

Since notifyAll() method wake up both the Waiter threads and program completes and terminates after execution. That’s all for wait, notify and notifyAll in java.

由于notifyAll()方法同時喚醒了Waiter線程,因此程序在執行后完成并終止。 這就是所有等待,java中的notify和notifyAll。

翻譯自: https://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183328.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息