Java多线程(5)--线程通信wait和notify
例題:使用兩個線程打印 1-100。線程1, 線程2 交替打印。
解決:涉及wait()和notify()/notifyAll()
class Communicate implements Runnable {private int number = 1;@Overridepublic void run() {while (true) {synchronized (this) {this.notify();if (number <= 100){System.out.println(Thread.currentThread().getName() + "打印出" + number);number++;try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}else break;}}} }public class CommunicateTest {public static void main(String[] args) {Communicate communicate = new Communicate();Thread thread1 = new Thread(communicate);Thread thread2 = new Thread(communicate);thread1.setName("線程一");thread2.setName("線程二");thread1.start();thread2.start();} }wait():令當前線程掛起,放棄CPU、同步資源并等待,使別的線程可訪問并修改共享資源,而當前線程排隊等候其他線程調用notify()或notifyAll()方法喚醒,喚醒后等待重新獲得對監視器的所有權后才能繼續執行。
notify():喚醒正在排隊等待同步資源的線程中優先級最高者結束等待。
notifyAll():喚醒正在排隊等待資源的所有線程結束等待。
這三個方法只有在synchronized方法或synchronized代碼塊中才能使用,否則會報
java.lang.IllegalMonitorStateException異常。
這三個方法的調用者都必須是同步監視器,即如同步監視器.wait()。例如以上代碼中的同步監視器是本對象this。
這三個方法是定義在Object類中的,而非Thread。
sleep() 和wait()的異同:
1.相同點:一 旦執行方法,都可以使得當前的線程進入阻塞狀態。
2.不同點:①兩個方法聲明的位置不同: Thread類中聲明sleep() ,Object類中聲明wait()。②調用的要求不同: sleep() 可以在任何需要的場景下調用,wait( )必須使用在同步代碼塊或同步方法中。③關于是否釋放同步監視器: 如果兩個方法都使用在同步代碼塊或同步方法中,sleep()不會釋放鎖,wait()會釋放鎖。
經典例題:生產者/消費者問題
生產者(Productor)將產品交給店員(Clerk),而消費者(Customer)從店員處取走產品,店員一次只能持有固定數量的產品(比如:20),如果生產者試圖生產更多的產品,店員會叫生產者停一下,如果店中有空位放產品了再通知生產者繼續生產;如果店中沒有產品了,店員會告訴消費者等一下,如果店中有產品了再通知消費者來取走產品。
①店員(產品):
//店員(產品) class Clerk {private int productNum = 0;public synchronized void addProduct() {if (productNum < 20){productNum++;System.out.println(Thread.currentThread().getName() + ":生產的第" + productNum + "個產品");notifyAll();}else {try {wait();} catch (InterruptedException e) {e.printStackTrace();}}}public synchronized void getProduct() {if (productNum > 0){productNum--;System.out.println(Thread.currentThread().getName() + ":消費的第" + productNum + "個產品");notifyAll();}else {try {wait();} catch (InterruptedException e) {e.printStackTrace();}}} }②生產者:
//生產者 class Productor implements Runnable {private Clerk clerk;public Productor(Clerk clerk) {this.clerk = clerk;}@Overridepublic void run() {System.out.println("生產者開始生產產品");while (true) {try {Thread.sleep((int) Math.random() * 1000);} catch (InterruptedException e) {e.printStackTrace();}clerk.addProduct();}} }③消費者:
//消費者 class Consumer implements Runnable {private Clerk clerk;public Consumer(Clerk clerk) {this.clerk = clerk;}public void run() {System.out.println("消費者開始取走產品");while (true) {try {Thread.sleep((int) Math.random() * 1000);} catch (InterruptedException e) {e.printStackTrace();}clerk.getProduct();}} }④主進程main
public class ProductTest {public static void main(String[] args) {Clerk clerk = new Clerk();Productor productor = new Productor(clerk);Consumer consumer = new Consumer(clerk);Thread thread1 = new Thread(productor);Thread thread2 = new Thread(consumer);thread1.setName("生產者");thread2.setName("消費者");thread1.start();thread2.start();} }總結
以上是生活随笔為你收集整理的Java多线程(5)--线程通信wait和notify的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 专利备案查询系统(专利备案查询)
- 下一篇: Java多线程(6)--Callable