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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 男人你得有范 中级黑马   /  2014-8-29 23:43  /  884 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
* 线程间通讯
* 多个线程同时操作同一个资源
*
* wait
* notify
* notifyAll
*
* 都使用在同步中,因为要对持有监视器(锁)的线程操作
* 所以要使用在同步中,因为只有同步才具有锁
*
* 为什么这些操作线程的方法要定义在Object类中呢?
* 因为这些方法操作同步线程时,都必须要标识它们所操作线程只有的锁,
* 只有同一个锁上的被等待线程才可以被同一个锁上notify唤醒
* 不可以对不同锁中的线程进行唤醒
*
* 也就是说,等待和唤醒必须是同一个锁
*
* 而锁可以是任意对象,所以可以被任意对象调用的的方法定义Object类中
*
*
* 优化后的代码
* */
  1. class Res2
  2. {
  3.         private String name;
  4.         private String sex;
  5.         private boolean flag = false;

  6.         public synchronized void set(String name, String sex)
  7.         {
  8.                 if (flag)
  9.                 {
  10.                         try
  11.                         {
  12.                                 this.wait();
  13.                         }
  14.                         catch (Exception e)
  15.                         {
  16.                         }
  17.                 }
  18.                 this.name = name;
  19.                 this.sex = sex;
  20.                 this.flag = true;
  21.                 this.notify();
  22.         }

  23.         public synchronized void print()
  24.         {
  25.                 if (!flag)
  26.                 {
  27.                         try
  28.                         {
  29.                                 this.wait();
  30.                         }
  31.                         catch (Exception e)
  32.                         {
  33.                         }
  34.                 }
  35.                 System.out.println(name + "------" + sex);
  36.                 this.flag = false;
  37.                 this.notify();
  38.         }
  39. }

  40. class Input2 implements Runnable
  41. {
  42.         private Res2 r;

  43.         Input2(Res2 r)
  44.         {
  45.                 this.r = r;
  46.         }

  47.         @Override
  48.         public void run()
  49.         {
  50.                 int x = 0;
  51.                 while (true)
  52.                 {
  53.                         if (x == 0)
  54.                                 r.set("gaozhihao", "man");
  55.                         else
  56.                                 r.set("高亚乐", "女");
  57.                         x = (x + 1) % 2;
  58.                 }
  59.         }
  60. }

  61. class Output2 implements Runnable
  62. {
  63.         private Res2 r;

  64.         Output2(Res2 r)
  65.         {
  66.                 this.r = r;
  67.         }

  68.         @Override
  69.         public void run()
  70.         {
  71.                 while (true)
  72.                 {
  73.                         r.print();
  74.                 }
  75.         }

  76. }

  77. public class InputOutputDemo2
  78. {
  79.         public static void main(String[] args)
  80.         {
  81.                 Res2 r = new Res2();

  82.                 new Thread(new Input2(r)).start();
  83.                 new Thread(new Output2(r)).start();
  84.         }
  85. }
复制代码

评分

参与人数 1黑马币 +1 收起 理由
格子、 + 1

查看全部评分

4 个回复

正序浏览
没有注释的代码就像没有颜色的绘画
回复 使用道具 举报
编写习惯很重要 最好开头能多行注释写下你的思路 然后编程的时候记得写注释 好习惯从开始养成~
回复 使用道具 举报
华谦 来自手机 中级黑马 2014-8-30 23:43:04
藤椅
嗯,楼上的说的是,没注释很头痛
回复 使用道具 举报
代码最好有注释
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马