本帖最后由 范龙波 于 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)    
 
自己看了很多遍可是没找到问题出在了哪里求明白人指点谢谢 */ 
 
 |   
        
 
    
    
    
     
 
 |