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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 范龙波 高级黑马   /  2013-4-23 21:55  /  778 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 范龙波 于 2013-4-23 22:04 编辑

class ZiYuan
{
        String name;
        String sex;
        boolean y=false;
}
class Input implements Runnable      //输入函数
{
        private ZiYuan r;
        
        Input(ZiYuan r)
        {
                this.r=r;
        }
        public void run()
        {
                int x=0;
                while(true)
                {
                                synchronized(r)
                                {
                                        if(r.y)
                                                try{r.wait();}catch(Exception e){};
                                       
                                        if(x==0)
                                        {
                                                r.name="张三";
                                                r.sex="男";
                                        }
                                                
                                        else
                                        {
                                                r.name="LILI";
                                                r.sex="nvnvnvnv";
                                        }
                                        x=(x+1)%2;
                                       
                                 }
                                r.y=true;     //终于找到了,问题出现在了这里自己找到了,原来写到括号外面了。
                                r.notify();
                }
     }
}               
class Output implements Runnable      //打印函数
{
        private ZiYuan r;
        Output(ZiYuan r)
        {
                this.r=r;        
        }
        public void run()
        {
                while(true)
                {
                        synchronized(r)
                        {        
                                if(!r.y)
                                        try{r.wait();}catch(Exception e){}
                                System.out.println(r.name+"\t"+r.sex);
                                r.y=false;
                               r.notify();
                                
                                
                        }
                }
               
        }
}

class   InOutput
{
        public static void main(String[] args)
        {
                ZiYuan r=new ZiYuan();
                Input in=new Input(r);
                Output out=new Output(r);
                Thread a=new Thread(in);
                Thread b=new Thread(out);
                a.start();
                b.start();

        }
}                                   

/*文件编译的时候没有问题,可是运行的时候却提示Exception in thread "Thread-0" 张三     男java.lang.IllegalMonitorStateException
     at java.lang.Object.notify(Native Method)
        at Input.run(InOutputDemo.java:38)
        at java.lang.Thread.run(Thread.java:662)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at Output.run(InOutputDemo.java:62)
        at java.lang.Thread.run(Thread.java:662)   

自己看了很多遍可是没找到问题出在了哪里求明白人指点谢谢 */

1 个回复

倒序浏览
自己重新看了 n遍最后终于发现了问题所在。原来写在了synchronized 括号的外面来了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马