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

© 潘廖明 中级黑马   /  2013-4-22 00:04  /  2562 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 潘廖明 于 2013-4-23 15:26 编辑

一、源程序:
  1. package xiancheng;

  2. public class YouhuaThreadCommunity
  3. {
  4.         /*
  5.          * 线程通讯:其实就是多个线程操作同一块资源,但是操作的行为不同
  6.          *  需求:一个人往箱子里装东西,一个人往箱子取东西。
  7.          *  东西:Res{name,sex;}
  8.          * 输入类:Input{Res,run()}
  9.          * 输出类: Oupt{Res,run()}
  10.          */
  11.         public static void main(String[] args)
  12.         {
  13.                 Res1 r = new Res1();
  14.                 Input1 in = new Input1(r);
  15.                 Output1 out = new Output1(r);
  16.                 new Thread(in).start();
  17.                 new Thread(out).start();
  18.                 Input1 in1 = new Input1(r);
  19.                 Output1 out1 = new Output1(r);
  20.                 new Thread(in1).start();
  21.                 new Thread(out1).start();
  22.         }
  23. }

  24. /*
  25. * 实现资源输出
  26. */
  27. class Input1 implements Runnable
  28. {
  29.         Res1 res;

  30.         /**
  31.          * 初始化资源
  32.          *
  33.          * @param r
  34.          */
  35.         public Input1(Res1 r)
  36.         {
  37.                 this.res = r;
  38.         }

  39.         /*
  40.          * 复写run()方法,生产资源
  41.          */
  42.         @Override
  43.         public void run()
  44.         {
  45.                 int x = 0;
  46.                 while (true)
  47.                 {
  48.                         res.setName("coke", "man");
  49.                         res.setName("笑话", "女");
  50.                 }

  51.         }

  52. }

  53. class Output1 implements Runnable
  54. {

  55.         Res1 res;

  56.         /*
  57.          * 初始化输出资源
  58.          */
  59.         public Output1(Res1 r)
  60.         {
  61.                 this.res = r;
  62.         }

  63.         /*
  64.          * 复写run()方法,消费资源
  65.          */
  66.         @Override
  67.         public void run()
  68.         {

  69.                 while (true)
  70.                 {
  71.                         res.show();
  72.                 }

  73.         }
  74. }

  75. class Res1
  76. {
  77.         private String name;
  78.         private String sex;
  79.         private boolean flag = false;

  80.         /**
  81.          * 设置资源的name与sex的值。
  82.          *  模拟生产资源
  83.          *
  84.          * @param name
  85.          * @param sex
  86.          */
  87.         public synchronized void setName(String name, String sex)
  88.         {
  89.                 while (flag)
  90.                         try
  91.                         {
  92.                                 this.wait();//冻结
  93.                         } catch (InterruptedException e)
  94.                         {
  95.                                 e.printStackTrace();
  96.                         }
  97.                 this.name = name;
  98.                 this.sex = sex;
  99.                 flag = true;
  100.                 this.notifyAll();//唤醒全部冻结线程
  101.         }

  102.         /**
  103.          * 模拟消费资源
  104.          */
  105.         public synchronized void show()
  106.         {
  107.                 while (!flag)
  108.                         try
  109.                         {
  110.                                 this.wait();//冻结
  111.                         } catch (InterruptedException e)
  112.                         {
  113.                                 e.printStackTrace();
  114.                         }
  115.                 System.out.println(name + "---消费---" + sex);
  116.                 flag = false;
  117.                 this.notifyAll();//唤醒全部冻结线程

  118.         }

  119. }
复制代码
二、问题如下:
为什么第一句输出“笑话---消费---女”不是成对的出现?
笑话---消费---女
coke---消费---man
coke---消费---man
笑话---消费---女
笑话---消费---女
coke---消费---man
coke---消费---man
笑话---消费---女
笑话---消费---女

评分

参与人数 1技术分 +1 收起 理由
张熙韬 + 1

查看全部评分

8 个回复

倒序浏览
main函数里面这样写:
  1.                 Res1 r = new Res1();
  2.                 Input1 in = new Input1(r);
  3.                 Output1 out = new Output1(r);
  4.                 new Thread(in).start();
  5.                 new Thread(out).start();
  6.                
  7.                 Res1 r1 = new Res1();
  8.                 Input1 in1 = new Input1(r1);
  9.                 Output1 out1 = new Output1(r1);
  10.                 new Thread(in1).start();
  11.                 new Thread(out1).start();
复制代码
两个不同的消费关系应该使用两个不同的资源

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
  1. package xiancheng;

  2. public class YouhuaThreadCommunity
  3. {
  4.         /*
  5.          * 线程通讯:其实就是多个线程操作同一块资源,但是操作的行为不同
  6.          *  需求:一个人往箱子里装东西,一个人往箱子取东西。
  7.          *  东西:Res{name,sex;}
  8.          * 输入类:Input{Res,run()}
  9.          * 输出类: Oupt{Res,run()}
  10.          */此处Output
复制代码

改完后运行下没事了

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
肖川 发表于 2013-4-22 23:47
main函数里面这样写:两个不同的消费关系应该使用两个不同的资源

本来就是要解决共享资源的问题。你这样分开没意思了!
回复 使用道具 举报
wodeairenw 发表于 2013-4-23 01:35
你建立了两个输入线程,两个取出线程,你可以定义一下线程的名称获取String name =Thread.currentThread(). ...

代码就是从老师那里看的,老师那边运行没事,怎么我的就是第一句没有匹配的?
回复 使用道具 举报
潘廖明 发表于 2013-4-23 01:42
本来就是要解决共享资源的问题。你这样分开没意思了!

你多运行几次就会得到不同的结果。可能与你的cpu运行速度有关,你一个生产者生产出来后还没来得及消费就开始了第二个生产.....
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马