返回列表 發帖

執行緒 (二)

以主類別繼承Thread類別,建立執行緒。

  1. public class Ch122 extends Thread {
  2.         /*
  3.         Ch122()
  4.         {
  5.                 start();       
  6.         }
  7.         */
  8.        
  9.         public void run()
  10.         {
  11.                 for(int i=5; i>=1; i--)
  12.                 {
  13.                         System.out.println(i+"秒");
  14.                         try {
  15.                                 sleep(1000);
  16.                         } catch (InterruptedException e) {}
  17.                 }
  18.                 System.out.println("時間到!");
  19.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());       
  20.         }
  21.        
  22.         public static void main(String[] args) {
  23.                 Ch122 app=new Ch122();
  24.                 app.start();
  25.                 //app.run();
  26.         }
  27. }
複製代碼

返回列表