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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 海世山盟 中级黑马   /  2014-3-29 17:25  /  839 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Res{
        private String name;
        private String sex;
        boolean flag=false;
        public  synchronized void set(String name,String sex)
        {
                if(flag)
                try{this.wait();}catch(Exception e){}
                this.name=name;
                this.sex=sex;
                flag=true;
                this.notify();
                }
                public  synchronized void out(){
                        if(!flag)
                        try{this.wait();}catch(Exception e){}
                        System.out.println(name+"......"+sex);
                        flag=false;
                        this.notify();
                        }
}

class Input implements Runnable{
        Res r;
        static int x=0;
        public Input(Res r){
                this.r=r;
        }
        public void run(){
                for(;x<=100;x=(x+1)){
       
                //        if(r.flag)
                                //try{r.wait();}catch(Exception e){}
                if(x%2==0){
                r.set("liming","man");
                }
                else
                {
                r.set("李明","男");                                                                                                                                                                                                                  
               
       
}
   
                //r.flag=false;
                //r.notify();
                }
               
        }
}


class Output implements Runnable{
        Res r;
        public Output(Res r){
                this.r=r;
        }
        public void run(){
                for(int x=0;x<=100;x++){
                               
                                        //if(!r.flag)
                                                //try{r.wait();}catch(Exception e){}
                r.out();
                //r.flag=true;
               
        }
        }
        }
       

public class contact {

        public static void main(String[] args) {
                //Res s=new Res();
                //Input in=new Input(s);
                //Output out =new Output(s);
                //Thread t1=new Thread(in);
                //Thread t2=new Thread(out);
       
               
                new Thread(new Input(new Res())).start();//Res我也用了匿名对象代替了
                new Thread(new Output(new Res())).start();//
                //t1.start();
                //t2.start();
                // TODO 自动生成的方法存根

        }

}

匿名对象会分配内存吗?如果分配的话,我只new Res(),一次说明内存中只有一个 Res的资源,那么当input 和output对Res资源进行操作的时候应该不会出错的啊?   这时你可能会说 当我们只对资源操作一次时 才用匿名对象。那么请看下面:
class QQ{
public void show(){
System.out.println("hello");
}
}
class opq{
        static int x;
public static void main(String[] args){
//匿名对象调用方法多次
        for(x=0;x<=10;x++){
  new QQ().show();
}
}
}
如果匿名对象只能被操作一次的话,这个为什么又可以被多次操作?
  

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

2 个回复

倒序浏览
你这都匿名了11个对象了..........不是一个
回复 使用道具 举报 1 0
osully 发表于 2014-3-29 17:56
你这都匿名了11个对象了..........不是一个

哎呀。好像真的是哦。。豁然开朗了。。上面的好像也是这个问题。匿名了一堆 Res。所以input output搞不定了。。。
谢谢了啊。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马