黑马程序员技术交流社区

标题: 这个能运行吗? [打印本页]

作者: 邓熊财    时间: 2015-3-23 12:54
标题: 这个能运行吗?
class Num
{        private String name;
        private String age;
        private boolean flag = false;
        public synchronized void set(String name , String age)
        {
                if(flag)
                {
                        try{this.wait();}catch(Exception e){}
                        this.name = name;
                        this.age = age;
                        flag = true;
                        this.notify();
                }
       
        }
        public synchronized void out()
        {
                if(!flag)
                {        try{this.wait();}catch(Exception e){}
                        System.out.println(name+"name"+",,,,,,"+age+"age");
                        flag = false;
                        this.notify();
                }
        }
       
}
class Nu implements Runnable
{        Num bbs;
        Nu(Num bbs)
        {
                this.bbs=bbs;
        }
        public void run()
        {
                int x = 0;
                while(true)
                {
                       
                        if (x==0)
                        {
                                bbs.set("名字","性别");
                        }else
                        {
                                bbs.set("lisi","坏人");
                        }
                        x = (x+1)%2;
                }
        }
}
class Num1        implements Runnable
{
        private Num bbs;
        Num1(Num bbs)
        {
                this.bbs=bbs;
        }
        public void run ()
        {  while(true)
                {
                        bbs.out();
                }
        }

}
class YouHua
{
        public static void main(String[] args)
        {
                Num bbs = new Num();
                new Thread(new Nu(bbs)).start();
                new Thread(new Num1(bbs)).start();
        }
}

作者: 邓熊财    时间: 2015-3-23 13:24
为什么我运行的时候是撒也没有呢?
作者: 关山明月    时间: 2015-3-23 14:18
Num 类中的set方法和out方法中的判断有问题,if判断后面不要大括号

  1. public synchronized void set(String name , String age)
  2.         {
  3.                 if(flag)
  4.                
  5.                         try{this.wait();}catch(Exception e){}
  6.                         this.name = name;
  7.                         this.age = age;
  8.                         flag = true;
  9.                         this.notify();
  10.                
  11.         
  12.         }
  13.         public synchronized void out()
  14.         {
  15.                 if(!flag)
  16.                       try{this.wait();}catch(Exception e){}
  17.                         System.out.println(name+"name"+",,,,,,"+age+"age");
  18.                         flag = false;
  19.                         this.notify();
  20.                
  21.         }
复制代码


set:当flag 为true时,线程等待。为false时,修改属性值(name,age,flag),唤醒其他等待线程
out:当flag 为true时,打印属性值(name,age),修改flag为false,唤醒其他等待线程。当为false时,线程等待
作者: 邓熊财    时间: 2015-3-23 14:18
有爱的人快来帮帮我撒

作者: 小鹏_sJQBO    时间: 2015-3-23 15:18
多线程的,晕晕的~~~
作者: 邓熊财    时间: 2015-3-23 15:26
谢谢
能运行吗?


作者: tubao1991    时间: 2015-3-23 15:32
貌似,if判断出了差错。
作者: 邓熊财    时间: 2015-3-23 15:42
怎么更改呢?
作者: poppy    时间: 2015-4-6 21:29
我都晕了
作者: D.man    时间: 2015-5-7 18:34
来学习学习,路过来看看的。
作者: 张伟1942    时间: 2015-5-7 18:35
运行不了,语句错误了
作者: wangyu    时间: 2015-5-7 19:35
不想看,太多了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2