返回列表 發帖
  1. public class Main {

  2.         public static void main(String[] args) {
  3.                 // TODO 自動產生的方法 Stub
  4.                 test();
  5.                 test2();
  6.                

  7.         }
  8.         public static void test()
  9.         {
  10.                 Thread t = new Thread()
  11.                 {
  12.                         public void run()
  13.                         {
  14.                                 for(int i=5;i>=1;i--)
  15.                                 {
  16.                                         System.out.println(i+"秒!"+Thread.currentThread().getName());
  17.                                         try {
  18.                                                 Thread.sleep(1000);
  19.                                         } catch (InterruptedException e) {
  20.                                                 // TODO 自動產生的 catch 區塊
  21.                                                 e.printStackTrace();
  22.                                         }
  23.                                 }
  24.                                 System.out.println("時間到!"+Thread.currentThread().getName());
  25.                         }
  26.                 };
  27.                 t.start();
  28.         }
  29.         public static void test2()
  30.         {
  31.                 Thread t2 = new Thread()
  32.                 {
  33.                         public void run()
  34.                         {
  35.                                 for(int i=1;i<=5;i++)
  36.                                 {
  37.                                         System.out.println(i+"秒!"+Thread.currentThread().getName());
  38.                                         try {
  39.                                                 Thread.sleep(1000);
  40.                                         } catch (InterruptedException e) {
  41.                                                 // TODO 自動產生的 catch 區塊
  42.                                                 e.printStackTrace();
  43.                                         }
  44.                                 }
  45.                                 System.out.println("時間到!"+Thread.currentThread().getName());
  46.                         }
  47.                 };
  48.                 t2.start();
  49.         }
  50. }
複製代碼

TOP

返回列表