多线程的线程通信(生产消费)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                多线程的线程通信(生产消费)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                消費者線程
package com.bjsxt.commu5;/*** 消費者線程*/ public class ConsumeRunnable implements Runnable {//private Product product = new Product();private Product product;private Object obj = new Object();public ConsumeRunnable() {}public ConsumeRunnable(Product product) {this.product = product;}public void setProduct(Product product) {this.product = product;}@Overridepublic void run() {while(true){product.consume();}} }生產者線程
package com.bjsxt.commu5;/*** 生產者線程*/ public class ProduceRunnable implements Runnable {//private Product product = new Product();private Product product;public ProduceRunnable() {}public ProduceRunnable(Product product) {this.product = product;}public void setProduct(Product product) {this.product = product;}@Overridepublic void run() {int i = 0;while(true){product.produce(i);i++;}} }產品
package com.bjsxt.commu5;import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;/*** 商品類*/ public class Product {private String name;//名稱 饅頭 玉米餅private String color;//顏色 白色 黃色boolean flag = false;//默認沒有商品private Lock lock = new ReentrantLock();private Condition produceCondition = lock.newCondition();private Condition consumeCondition = lock.newCondition();public Product() {}public Product(String name, String color) {this.name = name;this.color = color;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}@Overridepublic String toString() {return "Product{" +"name='" + name + '\'' +", color='" + color + '\'' +'}';}/*** 生產一個商品*/public void produce(int i){//thislock.lock();try{//1.如果已經有商品,就等待if(flag){try {//this.wait();produceCondition.await();} catch (InterruptedException e) {e.printStackTrace();}}//2. 生產商品并輸出if(i%2 ==0 ){//product.setName("饅頭");this.name = "饅頭";try {Thread.sleep(10); //只釋放cpu不釋放鎖} catch (InterruptedException e) {e.printStackTrace();}//product.setColor("白色");this.color = "白色";}else{//product.setName("玉米餅");this.name = "玉米餅";try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}//product.setColor("黃色");this.color = "黃色";}System.out.println("生產者生產一個商品:"+name+" "+color);//3.改變商品有無的狀態:有商品了this.flag = true;//4.通知消費//this.notify();//隨機的喚醒一個線程(目前只有兩個,被喚醒的肯定是消費者)consumeCondition.signal();}finally {lock.unlock();}}/*** 消費一個商品*/public void consume(){//thislock.lock();try{//1.如果沒有商品,就等待if(!flag){try {//this.wait(); //讓出了cpu,也讓出了鎖//wait();consumeCondition.await();} catch (InterruptedException e) {e.printStackTrace();}}//2.消費商品System.out.println("消費者消費一個商品:"+name+" "+color);//3.改變商品的有無狀態:無this.flag = false;//4.通知生產者生產//this.notifyAll();//喚醒所有等待的線程(目前所有等待的線程只有一個,就是消費者)produceCondition.signalAll();}finally {lock.unlock();}}}main
package com.bjsxt.commu5;public class Test {public static void main(String[] args) {Product product = new Product();//創建線程對象Runnable runnable1 = new ProduceRunnable(product);Thread thread1 = new Thread(runnable1);ConsumeRunnable runnable2 = new ConsumeRunnable();runnable2.setProduct(product);Thread thread2 = new Thread(runnable2);//啟動線程thread1.start();thread2.start();} }遇到的問題及解決方案:
- 問題1:生產和消費沒有交互
 - 解決:線程通信…
 - 問題2:和商品沒有關系
 - 解決:定義一個商品類
 - 問題3:生產者可以交替的生產,但是消費者消費的一直是null
 - 解決:保證生產者生產和消費者消費的是同一個商品
 - 問題4:出現了白色的玉米餅和黃色的饅頭
 - 解決:需要進行線程同步
 - 問題1:生產和消費沒有交互
 - 解決:線程通信…
 - 使用從Object類繼承的wait() notify() notifyAll()方法實現通信
 - 注意事項: 必須調用同步監視器的wait() notify() notifyAll()方法
 - product.wait();
 - product.notify();
 - product.notifyAll();
 
總結
以上是生活随笔為你收集整理的多线程的线程通信(生产消费)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 经济学专业实用什么电脑?
 - 下一篇: Kaboompics提供高质量、完全授权