[問題] 想要取得是由哪個子程序搶到時間

看板java作者 (陽光!?)時間19年前 (2006/05/02 15:20), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
我的程式碼如下 現在想要取得是由哪個子程序搶得cpu時間 (P1,P2) 有什麼辦法可以取得 假如說 我現在要取得是由哪個子程序搶得時間 然後 再讓二個子程序執行二個不同的程式 那要如何撰寫 (大部份內容都是由課本的內容修改 所以有很多不是很懂的地方) import java.awt.*; import java.awt.event.*; public class os_java extends Frame implements ActionListener { int stop=0; int pr1=0 , pr2=0 ; static os_java frm=new os_java(); static Button btn1=new Button("執行"); static Button btn2=new Button("停止"); static Button btn3=new Button("離開"); static TextArea txa1=new TextArea("",10,20,TextArea.SCROLLBARS_NONE); public static void main (String args[]) { txa1.setEditable(false); btn1.addActionListener(frm); btn2.addActionListener(frm); btn3.addActionListener(frm); frm.setTitle("OS_多執行緒"); frm.setLayout(new FlowLayout(FlowLayout.CENTER)); frm.setSize(400,300); frm.add(btn1); frm.add(btn2); frm.add(btn3); frm.add(txa1); frm.setVisible(true); } class Ctest extends Thread //創造子程序 { private String id; public Ctest(String str) { id=str ; } public void run() { for (int i=0;i<30;i++)//每一個子程序 執行30次 { for(int j=0 ; j<100000000;j++); //無限迴圈 讓文字訊息延遲 { txa1.append(id+" running..\n"); } if (stop==1)//如果按了離開 stop() ; } } } public void actionPerformed(ActionEvent e) { Button btn=(Button) e.getSource(); Ctest p1=new Ctest("P1"); Ctest p2=new Ctest("P2"); if(btn==btn1) //如果按的是執行 { p1.start() ; //讓p1與p2這二個子程序去搶時間 p2.start() ; } else if(btn==btn2) //如果按的是停止 { stop=1; p1.stop(); p2.stop(); } else //如果按的是離開 System.exit(0); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.162.114.153
文章代碼(AID): #14LmWpZM (java)
文章代碼(AID): #14LmWpZM (java)