返回列表 發帖
  1. public class Ch121 {
  2.         private MyThread1 th1;
  3.         private MyThread2 th2;
  4.         Ch121()
  5.         {
  6.                 th1=new MyThread1();
  7.                 th1.start();
  8.                 th2=new MyThread2();
  9.                 th2.start();
  10.         }
  11.         
  12.         public static void main(String[] args) {
  13.                 new Ch121();        
  14.         }
  15. }

  16. class MyThread1 extends Thread
  17. {
  18.         public void run()
  19.         {
  20.                 for(int i=5; i>=1; i--)
  21.                 {
  22.                         System.out.println(i+"秒"+Thread.currentThread().getName());
  23.                         try {
  24.                                 Thread.sleep(1000);
  25.                         } catch (InterruptedException e) {}
  26.                 }
  27.                 System.out.println("時間到!");
  28.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());
  29.         }
  30. }
  31. class MyThread2 extends Thread
  32. {
  33.         public void run()
  34.         {
  35.                 for(int i=5; i>=1; i--)
  36.                 {
  37.                         System.out.println(i+"秒"+Thread.currentThread().getName());
  38.                         try {
  39.                                 Thread.sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("時間到!");
  43.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());
  44.         }
  45. }
複製代碼

TOP

返回列表