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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邓熊财 中级黑马   /  2015-3-23 12:54  /  1040 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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();
        }
}

11 个回复

倒序浏览
为什么我运行的时候是撒也没有呢?
回复 使用道具 举报
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时,线程等待
回复 使用道具 举报
有爱的人快来帮帮我撒
回复 使用道具 举报
多线程的,晕晕的~~~
回复 使用道具 举报
谢谢
能运行吗?

回复 使用道具 举报
貌似,if判断出了差错。
回复 使用道具 举报
怎么更改呢?
回复 使用道具 举报
我都晕了
回复 使用道具 举报
来学习学习,路过来看看的。
回复 使用道具 举报
运行不了,语句错误了
回复 使用道具 举报
不想看,太多了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马