线程的通信
線程通訊:一個線程完成了自己的任務(wù)時,要通知另外一個線程去完成另外一個任務(wù)
public class Demo1 {public static void main(String [] args){Product p = new Product ();Producer producer = new Producer (p);Customer customer = new Customer (p);producer.start ();customer.start ();}} class Product{double price;String name; } class Producer extends Thread{Product p;public Producer(Product p){this.p = p;}public void run(){int i =0;while(true){if(i%2 ==0){p.name="蘋果";try {Thread.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();}p.price=6.5;}else{p.name="香蕉";p.price=2.0;}System.out.println ("生產(chǎn)者生產(chǎn)了:"+p.name+" 價格是:"+p.price);i++;}} }class Customer extends Thread{Product p;public Customer(Product product){this.p = product;}public void run(){while(true){System.out.println ("消費者消費了:"+p.name+" 價格是:"+p.price);}}} 消費者消費了:蘋果 價格是:2.0 消費者消費了:蘋果 價格是:2.0 消費者消費了:蘋果 價格是:2.0 消費者消費了:蘋果 價格是:2.0 消費者消費了:蘋果 價格是:2.0?
解決價格錯亂問題-加synchronized關(guān)鍵字
public class Demo {public static void main(String [] args){Product p = new Product ();Producer producer = new Producer (p);Customer customer = new Customer (p);producer.start ();customer.start ();}} class Product{double price;String name; } class Producer extends Thread{Product p;public Producer(Product p){this.p = p;}public void run(){int i =0;while(true){synchronized (p) {if (i % 2 == 0) {p.name = "蘋果";try {Thread.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();}p.price = 6.5;} else {p.name = "香蕉";p.price = 2.0;}System.out.println ("生產(chǎn)者生產(chǎn)了:" + p.name + " 價格是:" + p.price);i++;}}} }class Customer extends Thread{Product p;public Customer(Product product){this.p = product;}public void run(){while(true) {synchronized (p) {System.out.println ("消費者消費了:" + p.name + " 價格是:" + p.price);}}}}?
要求:
生產(chǎn)者生產(chǎn)完一個產(chǎn)品之后就要等待消費者去消費,然后在生產(chǎn),
消費者消費完一個產(chǎn)品之后就要等待生產(chǎn)者去生產(chǎn)
?
?
wait() 等待 如果線程執(zhí)行了wait方法那么該線程會進(jìn)入等待的狀態(tài),等待狀態(tài)下的線程必須調(diào)用notify 方法才能喚醒
notify()喚醒 喚醒等待的線程
wait與notify要注意的事項
1、wait 方法與notify 方法是屬于object對象的
2、wait 方法與notify方法必須要在同步代碼塊或者是同步函數(shù)中才能使用
3、wait方法與notify方法必須要有鎖對象調(diào)用。
?
public class SaleTicket {public static void main(String [] args){Product p = new Product ();Producer producer = new Producer (p);Customer customer = new Customer (p);producer.start ();customer.start ();}} class Product{double price;String name;boolean flag = false;//產(chǎn)品生產(chǎn)完畢的標(biāo)志,默認(rèn)是沒有生產(chǎn)完畢的 } class Producer extends Thread{Product p;public Producer(Product p){this.p = p;}public void run(){int i =0;while(true){synchronized (p) {if(p.flag ==false) {if (i % 2 == 0) {p.name = "蘋果";try {Thread.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();}p.price = 6.5;} else {p.name = "香蕉";p.price = 2.0;}System.out.println ("生產(chǎn)者生產(chǎn)了:" + p.name + " 價格是:" + p.price);i++;p.flag=true;p.notify ();//喚醒消費者去消費}else{//已經(jīng)生產(chǎn)完畢,等待消費者先去消費try {p.wait ();} catch (InterruptedException e) {e.printStackTrace ();}}}}} }class Customer extends Thread{Product p;public Customer(Product product){this.p = product;}public void run(){while(true) {synchronized (p) {if(p.flag==true) {//產(chǎn)品已經(jīng)生產(chǎn)完畢System.out.println ("消費者消費了:" + p.name + " 價格是:" + p.price);p.flag = false;p.notify ();//喚醒生產(chǎn)者去生產(chǎn)}else{//產(chǎn)品還沒有生產(chǎn),應(yīng)該等待生產(chǎn)者先去生產(chǎn)try {p.wait ();//消費者也等待了。。} catch (InterruptedException e) {e.printStackTrace ();}}}}}} 生產(chǎn)者生產(chǎn)了:蘋果 價格是:6.5 消費者消費了:蘋果 價格是:6.5 生產(chǎn)者生產(chǎn)了:香蕉 價格是:2.0 消費者消費了:香蕉 價格是:2.0?
轉(zhuǎn)載于:https://www.cnblogs.com/zhou-test/p/9879905.html
總結(jié)
- 上一篇: 双目立体匹配经典算法之Semi-Glob
- 下一篇: 美国数学月刊问题18-10-31