看板 java 關於我們 聯絡資訊
import java.util.*; public class S60 { public static void main(String [] avgs) { threadCount threadExample=new threadCount(10);//建立物件 並傳入參數 threadExample.Start();//開始執行緒 } } class threadCount extends Thread { int sec; threadCount(int sec) { this.sec=sec;//傳入需要多少秒跳出的參數指定 } public void run() { try { for(int i=1;i<=sec;i++) {//計次迴圈 //System.out.print(i);//測試用看幾秒 Thread.sleep(1000);//執行一次停一秒 if(i>=sec) { throw new ArithmeticException();//超過時間拋出例外 } } } Catch(ArithmeticException e) { System.out.print("例外");//捕捉設定的例外 } Catch(Exception e) { System.out.print("執行緒掛了");//捕捉Sleep的執行緒有無 掛點 } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.250.108.16
elenaser:FYI. 有誤導請多指教. 看看是不是你想要達到的效果 09/09 15:18
tkcn:建議你如果要貼程式碼,最好重新排版並且只貼上重要部份 09/09 15:31
elenaser:可以教我排版嗎T.T 09/09 15:35