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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 晓月清晖明 中级黑马   /  2015-4-14 17:59  /  435 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Dome
{
        public String name;
        public String sex;
        public boolean flag=false;
       
        public synchronized void set(String name,String sex)
        {
                if(this.flag)
                        try{this.wait();}catch(Exception e){}
                this.name=name;
                this.sex=sex;
                this.flag=true;
                this.notify();
        }
        public synchronized void out()
        {
                if(!this.flag)
                        try{this.wait();}catch(Exception e){}
                System.out.println("姓名:"+this.name+"---------性别:"+this.sex);
                this.flag=false;
                this.notify();
        }
       
}
class InputDome implements Runnable
{
        public Dome d;
        InputDome(Dome d)
        {
                this.d=d;
        }
        public void run()
        {
                //int i=0;
                boolean f=true;
                while(true)
                {
                        if(f)
                        {
                                d.set("weixiaohui", "man");
                                f=false;
                        }
                        else
                        {
                                d.set("魏晓晖", "男");
                                d.name="魏晓晖";
                                d.sex="男";
                                f=true;
                        }
                }
                /*while(true)
                {
               
                        synchronized(d)
                        {
                                if(d.flag)
                                        try{d.wait();}catch(Exception e){}
                                if(f)
                                {
                                        d.name="weixiaohui";
                                        d.sex="man";
                                        f=false;
                                }
                                else
                                {
                                        d.name="魏晓晖";
                                        d.sex="男";
                                        f=true;
                                }
                                d.flag=true;
                                d.notify();
                                //i=(i+1)%2;
                        }
                       
                }*/
               
               
        }
}
class OutputDome implements Runnable
{
        public Dome d;
        OutputDome(Dome d)
        {
                this.d=d;
        }
        public void run()
        {
                while(true)
                {
                        d.out();
                }
                /*while(true)
                {
                        synchronized(d)
                        {
                                if(!d.flag)
                                        try{d.wait();}catch(Exception e){}
                                System.out.println("姓名:"+d.name+"---------性别:"+d.sex);
                                d.flag=false;
                                d.notify();
                        }

                }*/
        }
}
public class InputOutputDome {

        /**
         * @param args
         */
        public static void main(String[] args) {
                Dome d=new Dome();
                Thread t1=new Thread(new InputDome(d));
                Thread t2=new Thread(new OutputDome(d));
                t1.start();
                t2.start();

        }

}
大神不要笑哈,初来乍到,多多关照

4 个回复

倒序浏览
自己顶一下

点评

水平不一般  发表于 2015-4-14 18:04
回复 使用道具 举报 1 0
帅哥  大神  求拥抱  求交友  求暖心  你懂得
回复 使用道具 举报
顶楼上  楼上顶  跳楼赞啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马