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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宁超 黑马帝   /  2011-10-12 11:17  /  3097 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

许冉 黑马帝 2011-10-12 12:11:34
楼主
不过你原来的代码只要改一处
if (flag) {
                        try {
                                this.wait();
                        } catch (Exception e) {
                        } }  //把if 的结束括号放这里
                        this.name = name;
                        this.sex = sex;
                        flag = true;
                        this.notify();
              
回复 使用道具 举报
Input中d的r只是局部的,main方法中得r的属性一直没有变
回复 使用道具 举报
本帖最后由 宁超 于 2011-10-12 11:45 编辑
  1. class Res {
  2.         private String name;
  3.         private String sex;
  4.         private boolean flag = false;
  5.         public synchronized void Set(String name, String sex) {
  6.                 if (flag) {
  7.                         try {
  8.                                 this.wait();
  9.                         } catch (Exception e) {
  10.                         }
  11.                         this.name = name;
  12.                         this.sex = sex;
  13.                         flag = true;
  14.                         this.notify();
  15.                 }
  16.         }

  17.         public synchronized void Out() {
  18.                 if (!flag)
  19.                         try {
  20.                                 this.wait();
  21.                         } catch (Exception e) {
  22.                         }
  23.                 System.out.println(name + "......." + sex);
  24.                 flag = false;
  25.                 this.notify();
  26.         }
  27. }

  28. class Input implements Runnable {
  29.         private Res r;
  30.         Input(Res r) {
  31.                 this.r = r;
  32.         }
  33.         public void run() {
  34.                 int x = 0;
  35.                 while (true) {
  36.                         if (x == 0)
  37.                                 r.Set("mike", "man");
  38.                         else
  39.                                 r.Set("小红", "女");
  40.                         x = (x + 1) % 2;

  41.                 }

  42.         }
  43. }

  44. class Output implements Runnable {
  45.         private Res r;

  46.         Output(Res r) {
  47.                 this.r = r;
  48.         }

  49.         public void run() {
  50.                 while (true) {
  51.                         r.Out();
  52.                 }

  53.         }
  54. }

  55. public class ThreadWaitDemo {
  56.         public static void main(String args[]) {
  57.                 Res r = new Res();
  58.                 new Thread(new Input(r)).start();
  59.                 new Thread(new Output(r)).start();
  60.         }

  61. }
复制代码
看完比老师后视频写的。但是执行不出来。谁帮看下。

8 个回复

倒序浏览
许冉 黑马帝 2011-10-12 13:11:25
板凳
宁超 发表于 2011-10-12 12:32
找到问题了。if 那多了层大括号

恩,if管的太宽了
回复 使用道具 举报
宁超 黑马帝 2011-10-12 12:32:59
报纸
找到问题了。
  1. if (flag) {

  2.                         try {

  3.                                 this.wait();

  4.                         } catch (Exception e) {

  5.                         }

  6.                         this.name = name;

  7.                         this.sex = sex;

  8.                         flag = true;

  9.                         this.notify();

  10.                 }

复制代码
if 那多了层大括号

评分

参与人数 1技术分 +1 黑马币 +4 收起 理由
wangfayin + 1 + 4 赞一个!

查看全部评分

回复 使用道具 举报
许冉 黑马帝 2011-10-12 11:40:10
地板
new Thread(new Input(r)).start();
new Thread(new Input(r)).start();

弄了两个Input,没有Output

评分

参与人数 1技术分 +1 黑马币 +4 收起 理由
wangfayin + 1 + 4 赞一个!

查看全部评分

回复 使用道具 举报
宁超 黑马帝 2011-10-12 11:43:44
7#
我改了还是的。。
回复 使用道具 举报
许冉 黑马帝 2011-10-12 11:59:48
8#
本帖最后由 xuan 于 2011-10-12 12:08 编辑

上回错了

public synchronized void Set(String name, String sex) {
                while (flag) {      //最好换成while
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }}             //  while结束在这
                        this.name = name;
                        this.sex = sex;
                        flag = true;       //这里应该是true,我搞错了
                        this.notify();
               
                                System.out.println("--");
        }

        public synchronized void Out() {
                while (!flag){
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }}
                System.out.println(name + "......." + sex);
                flag = false;
                this.notify();
        }
回复 使用道具 举报
郭敏 黑马帝 2011-10-12 13:45:13
9#
Res 中Set方法。  线程在调有该方法时先判读flag的值, 如果为真才运行,为假则跳过! 初始值为 false, 即使Input 得到CPU执行权,也无法将值设置给Res类中的name,sex。而在Out 方法中。先判读 !flag ,如为真,则线程wait().

评分

参与人数 1黑马币 +4 收起 理由
wangfayin + 4 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马