※ 引述《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