zoukankan      html  css  js  c++  java
  • 线程之间的相互通信

    线程之间的相互通信

    notify() 若是没有线程在wait,那也没有任何问题

    1.wait()和notify() 需要在同步代码块使用
    2.wait和notify方法必须要有锁对象调用

    利用锁对象建立线程池,所以必须锁对象一致,不然会建立不同的线程池

      1 /*
      2  线程的通信,互相调用 两个线程
      3  需求:  生产者  消费者 , 生产一个产品   就消费一个产品 , 产品是共享的
      4 
      5 
      6  */
      7 //产品类
      8 class Product{
      9     
     10     String name;  //名字
     11     
     12     double price;  //价格
     13     
     14     boolean flag = false; //产品是否生产完毕的标识,默认情况是没有生产完成。
     15     
     16 }
     17 
     18 //生产者
     19 class Producer extends Thread{
     20     
     21     Product  p ;      //产品
     22     
     23     public Producer(Product p) {
     24         this.p  = p ;
     25     }
     26     
     27     @Override
     28     public void run() {
     29         int i = 0 ; 
     30         while(true){
     31          synchronized (p) {
     32             if(p.flag==false){
     33                  if(i%2==0){
     34                      p.name = "苹果";
     35                      p.price = 6.5;
     36                  }else{
     37                      p.name="香蕉";
     38                      p.price = 2.0;
     39                  }
     40                  System.out.println("生产者生产出了:"+ p.name+" 价格是:"+ p.price);
     41                  p.flag = true;
     42                  i++;
     43                  p.notifyAll(); //唤醒消费者去消费
     44             }else{
     45                 //已经生产 完毕,等待消费者先去消费
     46                 try {
     47                     p.wait();   //生产者等待
     48                 } catch (InterruptedException e) {
     49                     e.printStackTrace();
     50                 }
     51             }
     52              
     53         }    
     54       }    
     55     }
     56 }
     57 
     58 
     59 //消费者
     60 class Customer extends Thread{
     61     
     62     Product p; 
     63     
     64     public  Customer(Product p) {
     65         this.p = p;
     66     }
     67     
     68     @Override
     69     public void run() {
     70         while(true){
     71             synchronized (p) {    
     72                 if(p.flag==true){  //产品已经生产完毕
     73                     System.out.println("消费者消费了"+p.name+" 价格:"+ p.price);
     74                     p.flag = false; 
     75                     p.notifyAll(); // 唤醒生产者去生产
     76                 }else{
     77                     //产品还没有生产,应该 等待生产者先生产。
     78                     try {
     79                         p.wait(); //消费者也等待了...
     80                     } catch (InterruptedException e) {
     81                         e.printStackTrace();
     82                     }
     83                 }
     84             }
     85         }    
     86     }
     87 }
     88 
     89 
     90 public class Demo9 {
     91 
     92     public static void main(String[] args) {
     93         Product p = new Product();
     94         Producer pr = new Producer(p);
     95         Customer c = new Customer(p);
     96         pr.start();
     97         c.start();
     98 
     99     }
    100 
    101 }
  • 相关阅读:
    面试题:区分List中remove(int index)和remove(Object obj)
    Collection的子接口之一:List 接口
    面试题:ArrayList、LinkedList、Vector三者的异同?
    jdk 5.0 新增的foreach循环(用于遍历集合、数组)
    Iterator迭代器接口(遍历Collection的两种方式之一)
    哈希值
    Collection接口方法
    集合框架的概述
    注解(Annotation)
    System类、Math类、BigInteger与BigDecimal的使用
  • 原文地址:https://www.cnblogs.com/bequt/p/5655043.html
Copyright © 2011-2022 走看看