返回列表 發帖
  1. public class Ch124
  2. {
  3.         private MyThread1 th1;
  4.         private MyThread2 th2;
  5.         Ch124()
  6.         {
  7.                 th1=new MyThread1();
  8.                 th1.start();
  9.                 th2=new MyThread2();
  10.                 th2.start();
  11.         }
  12.         public static void main(String args[])
  13.         {
  14.                 new Ch124();
  15.         }
  16. }
  17. class MyThread1  extends Thread
  18. {
  19.         public void run(){
  20.                 for(int i=5;i>=1;i--)
  21.                 {
  22.                         System.out.println(i+"秒 "+Thread.currentThread().getName());
  23.                         try
  24.                         {
  25.                                 sleep(1000);
  26.                         }catch(InterruptedException e){}
  27.                        
  28.                 }
  29.                 System.out.println("時間到");
  30.                 System.out.println("執行緒名稱"+Thread.currentThread().getName());
  31.         }
  32. }
  33. class MyThread2 extends Thread
  34. {
  35.         public void run(){
  36.                 for(int i=5;i>=1;i--)
  37.                 {
  38.                         System.out.println(i+"秒 "+Thread.currentThread().getName());
  39.                         try
  40.                         {
  41.                                 sleep(1000);
  42.                         }catch(InterruptedException e){}
  43.                        
  44.                 }
  45.                 System.out.println("時間到");
  46.                 System.out.println("執行緒名稱"+Thread.currentThread().getName());
  47.         }
  48. }
複製代碼

TOP

返回列表