作者csihcs (非天夜翔)
看板java
标题Re: [问题] Thread的wait 与notify...
时间Sat Dec 6 03:01:28 2008
※ 引述《Gitangan (周大猫)》之铭言:
: 这只程式的流程为切到BoneView去画图,当中BoneView里有thread在run
: 然後切换到别的页面,switch里面的current设成别的值..
: 把BoneView的thread设成wait(),把run()里面的run设成false让run()停止不空转...
: 之前又要把画面切回BoneView让他继续画图,此时把BoneView的thread notify叫醒..
: 此时图就不再画了...
: 推测是把thread wait()掉时,把run()里面while回圈的参数值设成false,
: 所以while终止把thread的生命也终止掉了!?
: 想不到有什麽方法可以使run()复活继续跑...用t.start() 会出错..囧z
method :
Thread : interrupt(), sleep()
BoneView :
startPaint() {
if(sleeping) {
sleeping = false;
t.interrupt();
}
}
pausePaint() {
goingSleep = true;
}
stopPaint() {
stop = true;
goingSleep = true;
}
: public class BoneView implements Runnable(){
: ...
: Thread t = new Thread();
: boolean run = true;
: public void run(){ //thread run 的部分
while(!stop) {
: synchronized(this){
: ...
: while(!goingSleep){
: ...
: }
: }
if(goingSleep) { // 可能存在不同情况离开上面的回圈
try {
goingSleep = false;
sleeping = true;
t.sleep();
} catch (...) {
...
}
}
: }
: }
: ---程式runrunrun,然後把BoneView.t.wait()
: BoneView.run = false;
: 之後页面又切回BoneView
: switch(current){
: ...
: case bone:
: ...
: BoneView.run = true;
: BoneView.t.notify(); //复活thread
: //BoneView.t.notifyAll();
: ...
: display.setCurrent(BoneView.getInstance()); //把画面设定当前页面
: break;
: ...
: }
: 试过把display.setCurrent(BoneView.getInstance());
: 换成display.setCurrent(new BoneView());
: 就可以成功,但是好像不太符合效益!?每次都new一个...
: 所以只好重new了吗 囧z
: 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.104.29.87