線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1131
推到 Plurk!
推到 Facebook!

問個Thread相關的問題

尚未結案
Sephiroth105
一般會員


發表:13
回覆:5
積分:3
註冊:2003-05-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-28 23:43:58 IP:61.70.xxx.xxx 未訂閱
我現在有一個會停止的while loop while(....) { A(); ..... B(); C(); ...... } 其中我有一個variable再A,B,C三個function中都會去更動到 我希望有一個thread可以隨時中斷while loop中的任何一行 包括三個function都可以中斷 然後將變數(一個二為陣列用來畫圖)畫在圖上 我想做出能夠有演化效果的圖形呈現 請問該怎麼去使用thread比較好?
ahan
一般會員


發表:11
回覆:18
積分:15
註冊:2002-07-07

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-29 03:51:24 IP:210.71.xxx.xxx 未訂閱
提供一個懶人在用的方法… 在你想要『暫停』的地方加入下列程式碼 ================================== try {     Thread.sleep(1000);   //1000為毫秒 即等於1秒 } catch(Exception){} //================================== 這樣就會有暫停的效果囉 但若不想效率降低…可將畫圖的包成Thread,才不會整個程式都Sleep 或改用Timer也可…希望對你有幫助囉…
Sephiroth105
一般會員


發表:13
回覆:5
積分:3
註冊:2003-05-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-08-29 12:21:45 IP:61.70.xxx.xxx 未訂閱
起問我這樣寫合法嘛???    class dPanel extends JPanel {     int vArray[];        public dPanel(information for drawing)     {         vArray = information for drawing       .......     }        public synchronized void paint(Graphics g)     {         draw the graph on panel with the vArray;         ......         .....     } }    public class drawing {      dPanel dp ;     Timer timer;     int vArray[];     ...     ...     class RemindTask extends TimerTask     {         public void run()          {             dp = new dPanel(vArray);         }     }        public drawing()     {         ......         ......              timer.schedule(new RemindTask,0,100); //start timer                 while()  //finite while loop (do the nodes movement)         {             A();     //modify vArray             ...      //modify vArray             B();     //modify vArray         }     }    這樣寫可以做到每0.1秒更新目前的最新畫面一次嘛?????    因為我第一次使用timer這類thread的程式所以不太熟 希望大家幫忙,謝謝     發表人 - sephiroth105 於 2003/08/29 23:44:33
Sephiroth105
一般會員


發表:13
回覆:5
積分:3
註冊:2003-05-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-08-30 21:40:40 IP:61.70.xxx.xxx 未訂閱
我又改了另一種做法,可以跑可是畫面沒有任何更新動作 可以告訴我錯誤在哪嘛??? 主要概述如下: class mCanvas extends JPanel { node[] loc; public mCanvas(node[] in) { loc = in; } public void reDraw(node[] in) { loc = in; this.repaint(); } public void synchronized update(Graphics g) { //use variable 'loc' to paint on mCanvas } } public class mDrawing { node[] v; java.util.Timer timer; JFrame f; mCanvas dp; //inner class class RemindTask extends TimerTask { boolean first; public RemindTask() { first = true; } public void run() { if(first == true) { dp = new mCanvas(v); first = false; //show on frame f.setContentPane(dp); f.setVisible(true); } else { dp.reDraw(v); } } }; public mDrawing() { timer = new java.util.Timer(); timer.schedule(new RemindTask(),0,100); while()//finite while { //always modify the node[] v } timer.cancel(); } } 我想在finite while中每0.1秒畫出最新的圖形位置 爲什麼frame沒有任何動靜... 可以告訴我錯哪邊嘛?謝謝 發表人 - sephiroth105 於 2003/08/30 21:42:35 發表人 - sephiroth105 於 2003/08/30 21:45:39
系統時間:2024-05-20 7:41:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!