A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郭光明 中级黑马   /  2012-12-11 23:23  /  1489 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

模拟多线程 看了视频终于做出来了 不知道这样做是否可以 求大神指导

模拟妈妈做饭,做饭时发现没有盐了,让儿子去买盐(假设买盐需要3分钟), 只有盐买回来之后,妈妈才能继续做饭的过程
  1. public class Test10 {

  2.         /**
  3.          * @param args
  4.          *           
  5.          *
  6.          */
  7.         public static void main(String[] args) {

  8.                 Salt salt = new Salt();
  9.                 Mother m = new Mother(salt);
  10.                 Son s = new Son(salt);
  11.                 Thread t1 = new Thread(m);
  12.                 Thread t2 = new Thread(s);
  13.                 t1.start();
  14.                 try {
  15.                         Thread.sleep(1000);
  16.                 } catch (InterruptedException e) {
  17.                         // TODO Auto-generated catch block
  18.                         e.printStackTrace();
  19.                 }
  20.                 t2.start();

  21.         }

  22. }

  23. class Salt {

  24.         boolean falg = true;

  25. }

  26. class Mother implements Runnable {

  27.         private Salt salt;

  28.         Mother(Salt salt) {
  29.                 this.salt = salt;
  30.         }

  31.         public void run() {
  32.                 synchronized (salt) {
  33.                         System.out.println("Mother Cooking");

  34.                         if (salt.falg) {

  35.                                 System.out.println("Son Buy Salt");
  36.                                 try {
  37.                                         salt.wait();
  38.                                 } catch (InterruptedException e) {
  39.                                         // TODO Auto-generated catch block
  40.                                         e.printStackTrace();
  41.                                 }

  42.                         }

  43.                         System.out.println("Mother Cook");
  44.                         salt.notify();
  45.                 }

  46.         }

  47. }

  48. class Son implements Runnable {

  49.         private Salt salt;

  50.         Son(Salt salt) {
  51.                 this.salt = salt;
  52.         }

  53.         public void run() {
  54.                 synchronized (salt) {
  55.                         if (!salt.falg) {
  56.                                 try {
  57.                                         salt.wait();
  58.                                 } catch (InterruptedException e1) {
  59.                                         // TODO Auto-generated catch block
  60.                                         e1.printStackTrace();
  61.                                 }
  62.                                 System.out.println("Buy Salt");
  63.                                 try {
  64.                                         Thread.sleep(3 * 60000);
  65.                                 } catch (InterruptedException e) {
  66.                                         // TODO Auto-generated catch block
  67.                                         e.printStackTrace();
  68.                                 }
  69.                         }
  70.                         salt.falg = true;
  71.                         salt.notifyAll();
  72.                 }
  73.         }

  74. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马