zoukankan      html  css  js  c++  java
  • 多线程 wait(),notify()方法,案例总结

    废话不多说,案例如下

     1 package com.xujingyang.Exok;
     2 
     3 /**
     4  * 商品类
     5  * @author 徐景洋
     6  */
     7 public class Goods {
     8     private String pinpai;
     9     private String name;
    10     
    11     private int num;
    12     
    13     public int getNum() {
    14         return num;
    15     }
    16     public void setNum(int num) {
    17         this.num = num;
    18     }
    19     public String getPinpai() {
    20         return pinpai;
    21     }
    22     public void setPinpai(String pinpai) {
    23         this.pinpai = pinpai;
    24     }
    25     public String getName() {
    26         return name;
    27     }
    28     public void setName(String name) {
    29         this.name = name;
    30     }
    31     
    32 }
     1 package com.xujingyang.Exok;
     2 
     3 /**
     4  * 生产商类
     5  * @author 徐景洋
     6  */
     7 public class Producer implements Runnable {
     8     private Goods goods;
     9 
    10     public Goods getGoods() {
    11         return goods;
    12     }
    13 
    14     public void setGoods(Goods goods) {
    15         this.goods = goods;
    16     }
    17 
    18     @Override
    19     public void run() {
    20         for (int i = 0; i < 5; i++) {
    21             synchronized (goods) {
    22                 if(goods.getNum()>0){
    23                     try {
    24                         goods.wait();//商品数量已经大于0啦,消费者要取货咯,自己就开始等待咯
    25                     } catch (InterruptedException e) {
    26                         e.printStackTrace();
    27                     }
    28                 }
    29                 
    30                 if (i % 2 == 0) {
    31                     goods.setPinpai("哇哈哈");
    32                     try {
    33                         Thread.sleep(10);
    34                     } catch (InterruptedException e) {
    35                         e.printStackTrace();
    36                     }
    37                     goods.setName("矿泉水");
    38 
    39                 } else {
    40                     goods.setPinpai("旺仔");
    41                     try {
    42                         Thread.sleep(10);
    43                     } catch (InterruptedException e) {
    44                         e.printStackTrace();
    45                     }
    46                     goods.setName("小馒头");
    47                 }
    48                 goods.setNum((goods.getNum()+1));
    49                 System.out.println("生产了" + goods.getPinpai() + goods.getName());
    50                 goods.notify();//商品不够啦,自己生产完,然后通知消费者取货咯
    51             }
    52         }
    53     }
    54 
    55 }
     1 package com.xujingyang.Exok;
     2 
     3 /**
     4  * 消费者类
     5  * @author 徐景洋
     6  */
     7 public class Customer implements Runnable {
     8 
     9     private Goods goods;
    10 
    11     public Goods getGoods() {
    12         return goods;
    13     }
    14 
    15     public void setGoods(Goods goods) {
    16         this.goods = goods;
    17     }
    18 
    19     @Override
    20     public void run() {
    21         for (int i = 0; i < 5; i++) {
    22             synchronized (goods) {
    23                 if(goods.getNum()<=0){
    24                     try {//如果商品生产的数量小于0,则开始等待.只有有货才能购物嘛
    25                         goods.wait();
    26                     } catch (InterruptedException e) {
    27                         e.printStackTrace();
    28                     }
    29                 }
    30                 
    31                 try {
    32                     Thread.sleep(10);
    33                 } catch (InterruptedException e) {
    34                     e.printStackTrace();
    35                 }
    36                 goods.setNum((goods.getNum()-1));
    37                 System.out.println("取走了" + goods.getPinpai() + goods.getName());
    38                 goods.notify();//取走之后通知生产商继续生产商品(唤醒在对象锁等待池中的线程继续执行)
    39             }
    40         }
    41     }
    42 
    43 }
     1 package com.xujingyang.Exok;
     2 
     3 /**
     4  * 测试类
     5  * @author 徐景洋
     6  */
     7 public class Test {
     8     public static void main(String[] args) {
     9         Goods goods=new Goods();
    10         
    11         //生产者生产商品
    12         Producer p=new Producer();
    13         p.setGoods(goods);
    14         
    15         //消费者取走商品
    16         Customer c=new Customer();
    17         c.setGoods(goods);
    18         
    19         new Thread(p).start();
    20         new Thread(c).start();
    21     }
    22 }

    清晰明了不,嘿嘿

  • 相关阅读:
    Leetcode929.Unique Email Addresses独特的电子邮件地址
    Leetcode914.X of a Kind in a Deck of Cards卡牌分组
    new Date()设置日期在IOS的兼容问题
    koa2 使用passport权限认证中间件
    当 better-scroll 遇见 Vue
    百度地图开发
    通过Ajax方式上传文件,使用FormData进行Ajax请求
    app中页面滑动,防止a链接误触
    js判断是微信、QQ内置浏览器打开页面
    百度天气预报接口介绍
  • 原文地址:https://www.cnblogs.com/xujingyang/p/6572966.html
Copyright © 2011-2022 走看看