问题描述
小弟最近碰到一个现成等待的问题在CANVAS中覆盖了keyPressed方法中wait了一个线程然后我再按键这个事件一直没有被监听到这是什么原因?主要功能是实现了一个窗帘的移动代码如下Canvas类:packageorg.ray;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Graphics;publicclassCurtainControlextendsCanvas{privateintstep;privateintscreenW=this.getWidth();privateintscreenH=this.getHeight();privateintheight;privatebooleanflag;privateThreadRunt;publicCurtainControl(){step=(int)(screenH*0.01);height=0;flag=false;}publicvoidsetT(ThreadRunt){this.t=t;}protectedvoidkeyPressed(intkeyCode){this.repaint();switch(getGameAction(keyCode)){caseCanvas.FIRE:if(!flag)this.stop();elsethis.continueDo();break;caseCanvas.UP:if(step>=0)step=-step;break;caseCanvas.DOWN:if(step<=0)step=-step;break;}}protectedvoidpaint(Graphicsg){if(step<0){g.setColor(255,255,255);g.fillRect(0,0,this.screenW,this.screenH);}g.setColor(128,128,0);g.fillRect(0,0,screenW,height);g.setColor(184,134,11);intgap=(int)(this.screenW/20);intpreX=0;intthirdY=(int)(Math.sqrt((gap*gap)-(gap/2)*(gap/2)));inti=0;intthirdX=0;for(i=0;i<this.screenW;i=i+gap){thirdX=(int)((preX+i)/2);g.fillTriangle(preX,height,i,height,thirdX,height-thirdY);preX=i;}thirdX=(int)((preX+this.screenW)/2);g.fillTriangle(preX,height,this.screenW,height,thirdX,height-thirdY);}publicvoidstop(){this.flag=true;synchronized(t){try{t.wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}//t.stopJOB();}publicvoidcontinueDo(){this.flag=false;synchronized(t){t.notifyAll();}//t.startJOB();}publicsynchronizedvoidaddHeight(){if((height>getHeight())||(height<0)){step=-step;}height+=step;}}
线程类:packageorg.ray;importorg.ray.CurtainControl;publicclassThreadRunimplementsRunnable{privateCurtainControlcc;privatebooleanflag;publicThreadRun(CurtainControlcc){super();this.cc=cc;flag=true;}publicvoidrun(){//TODOAuto-generatedmethodstubwhile(true){if(flag){cc.addHeight();cc.repaint();try{Thread.sleep(100);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}else{try{Thread.sleep(200);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}publicvoidstopJOB(){flag=false;}publicvoidstartJOB(){synchronized(this){flag=true;}}}
解决方案
解决方案二:
主要问题是线程没启动,稍微做了些改动,不知道你是不是想实现这样的效果。importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Graphics;publicclassCurtainControlextendsCanvas{privateintstep;privateintscreenW=this.getWidth();privateintscreenH=this.getHeight();privateintheight;privatebooleanflag;privateThreadRunt;publicCurtainControl(){step=(int)(screenH*0.01);height=0;flag=false;setT();}publicvoidsetT(){this.t=newThreadRun(this);}protectedvoidkeyPressed(intkeyCode){this.repaint();switch(getGameAction(keyCode)){caseCanvas.FIRE:System.out.println("fire");System.out.println(flag);if(!flag)this.stop();elsethis.continueDo();break;caseCanvas.UP:if(step>=0)step=-step;break;caseCanvas.DOWN:if(step<=0)step=-step;break;}}protectedvoidpaint(Graphicsg){if(step<0){g.setColor(255,255,255);g.fillRect(0,0,this.screenW,this.screenH);}g.setColor(128,128,0);g.fillRect(0,0,screenW,height);g.setColor(184,134,11);intgap=(int)(this.screenW/20);intpreX=0;intthirdY=(int)(Math.sqrt((gap*gap)-(gap/2)*(gap/2)));inti=0;intthirdX=0;for(i=0;i<this.screenW;i=i+gap){thirdX=(int)((preX+i)/2);g.fillTriangle(preX,height,i,height,thirdX,height-thirdY);preX=i;}thirdX=(int)((preX+this.screenW)/2);g.fillTriangle(preX,height,this.screenW,height,thirdX,height-thirdY);}publicvoidstop(){this.flag=true;//synchronized(t){//try{//t.wait();//}catch(InterruptedExceptione){////TODOAuto-generatedcatchblock//e.printStackTrace();//}//}t.stopJOB();}publicvoidcontinueDo(){this.flag=false;////synchronized(t){//t.notifyAll();//}t.startJOB();}publicsynchronizedvoidaddHeight(){if((height>getHeight())||(height<0)){step=-step;}height+=step;}}classThreadRunimplementsRunnable{privateCurtainControlcc;privatebooleanflag;publicThreadRun(CurtainControlcc){super();this.cc=cc;flag=true;newThread(this).start();//主要是加了这句,让线程启动}publicvoidrun(){//TODOAuto-generatedmethodstubwhile(true){if(flag){cc.addHeight();cc.repaint();try{Thread.sleep(100);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}else{try{Thread.sleep(200);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}publicvoidstopJOB(){flag=false;}publicvoidstartJOB(){synchronized(this){flag=true;}}}
解决方案三:
uping
解决方案四:
这个是我的错没把外面的Medlet写出来packageorg.ray;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Display;importjavax.microedition.lcdui.Displayable;importjavax.microedition.midlet.MIDlet;importjavax.microedition.midlet.MIDletStateChangeException;publicclassMainletextendsMIDlet{publicMainlet(){//TODOAuto-generatedconstructorstub}protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{//TODOAuto-generatedmethodstub}protectedvoidpauseApp(){//TODOAuto-generatedmethodstub}protectedvoidstartApp()throwsMIDletStateChangeException{Displayd=Display.getDisplay(this);CurtainControlcc=newCurtainControl();ThreadRuntr=newThreadRun(cc);finalThreadt=newThread(tr);cc.setT(tr);cc.addCommand(newCommand("a",Command.EXIT,1));d.setCurrent(cc);t.start();}}
再说明白点。。就是线程WAIT了之后就无法唤醒了
解决方案五:
keyPressed本身也是个线程LZ不会把CurtainControl也锁死了吧,虽然没遇到过,但觉得有可能使这个问题
解决方案六:
publicvoidstop(){this.flag=true;synchronized(t){try{t.wait();}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}//t.stopJOB();}你觉得你这段执行后,这个方法能返回吗?你把系统主线程阻塞了.