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