日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

线程的通信

發布時間:2025/3/16 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程的通信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

線程通訊:一個線程完成了自己的任務時,要通知另外一個線程去完成另外一個任務

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 ("生產者生產了:"+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關鍵字

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 ("生產者生產了:" + 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);}}}}

?

要求:

生產者生產完一個產品之后就要等待消費者去消費,然后在生產,

消費者消費完一個產品之后就要等待生產者去生產

?

?

wait() 等待 如果線程執行了wait方法那么該線程會進入等待的狀態,等待狀態下的線程必須調用notify 方法才能喚醒

notify()喚醒 喚醒等待的線程

wait與notify要注意的事項

1、wait 方法與notify 方法是屬于object對象的

2、wait 方法與notify方法必須要在同步代碼塊或者是同步函數中才能使用

3、wait方法與notify方法必須要有鎖對象調用。

?

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;//產品生產完畢的標志,默認是沒有生產完畢的 } 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 ("生產者生產了:" + p.name + " 價格是:" + p.price);i++;p.flag=true;p.notify ();//喚醒消費者去消費}else{//已經生產完畢,等待消費者先去消費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) {//產品已經生產完畢System.out.println ("消費者消費了:" + p.name + " 價格是:" + p.price);p.flag = false;p.notify ();//喚醒生產者去生產}else{//產品還沒有生產,應該等待生產者先去生產try {p.wait ();//消費者也等待了。。} catch (InterruptedException e) {e.printStackTrace ();}}}}}} 生產者生產了:蘋果 價格是:6.5 消費者消費了:蘋果 價格是:6.5 生產者生產了:香蕉 價格是:2.0 消費者消費了:香蕉 價格是:2.0

?

轉載于:https://www.cnblogs.com/zhou-test/p/9879905.html

總結

以上是生活随笔為你收集整理的线程的通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。