问题描述
classBuffer2{privateintvalue;privatebooleanisEmpty=true;synchronizedvoidput(inti){while(!isEmpty){try{this.wait();}catch(InterruptedExceptione){}}}value=i;isEmpty=false;notify();}synchronizedintget(){while(isEmpty){try{this.wait();}catch(InterruptedExceptione){}isEmpty=true;notify();returnvalue;}}classput2extendsThread{privateBuffer2bf;publicput2(Buffer2bf){this.bf=bf;}publicvoidrun(){for(inti=1;i<6;i++){bf.put(i);System.out.println("put2put:"+i);}}publicstaticvoidmain(String[]args){Buffer2bf=newBuffer2();(newput2(bf)).start();(newput2(bf)).start();}}classGet2extendsThread{privateBuffer2bf;publicvoidGet(Buffer2bf){this.bf=bf;}publicvoidrun(){for(inti=1;i<6;i++)System.out.println("ttGetget:"+bf.get());}}编译出错,请高手帮忙一下
解决方案
解决方案二:
问题很多。说一下想做什么先。
解决方案三:
错误真多啊!!!publicclassBuffer2{privateintvalue;privatebooleanisEmpty=true;synchronizedvoidput(inti){//while(!isEmpty){if(!isEmpty){try{this.wait();}catch(InterruptedExceptione){e.printStackTrace();}}value=i;isEmpty=false;notify();}synchronizedintget(){//while(isEmpty){if(isEmpty){try{this.wait();}catch(InterruptedExceptione){}}isEmpty=true;notify();returnvalue;//}}}
publicclassPut2extendsThread{privateBuffer2bf;publicPut2(Buffer2bf){this.bf=bf;}publicvoidrun(){for(inti=1;i<6;i++){bf.put(i);System.out.println("put2put:"+i);}}publicstaticvoidmain(String[]args){Buffer2bf=newBuffer2();//(newput2(bf)).start();//(newput2(bf)).start();newPut2(bf).start();newGet2(bf).start();}}
publicclassGet2extendsThread{privateBuffer2bf;publicGet2(Buffer2bf){//publicvoidGet(Buffer2bf){this.bf=bf;}publicvoidrun(){for(inti=1;i<6;i++)System.out.println("ttGetget:"+bf.get());}}
解决方案四:
我果断的看晕了.....