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

  2.         public static void main(String[] args) {
  3.                 // TODO 自動產生的方法 Stub
  4.                 MyThread1 t = new MyThread1();
  5.                 MyThread2 t2 = new MyThread2();
  6.                 t.start();
  7.                 t2.start();
  8.         }

  9. }
複製代碼
  1. public class MyThread1 extends Thread{
  2.         public void run()
  3.         {
  4.                 for(int i=5;i>=1;i--)
  5.                 {
  6.                         System.out.println(i+"秒!"+Thread.currentThread().getName());
  7.                         try {
  8.                                 Thread.sleep(1000);
  9.                         } catch (InterruptedException e) {
  10.                                 // TODO 自動產生的 catch 區塊
  11.                                 e.printStackTrace();
  12.                         }
  13.                 }
  14.                 System.out.println("時間到!"+Thread.currentThread().getName());
  15.         }
  16. }
複製代碼
  1. public class MyThread2 extends Thread{
  2.         public void run()
  3.         {
  4.                 for(int i=1;i<=5;i++)
  5.                 {
  6.                         System.out.println(i+"秒!"+Thread.currentThread().getName());
  7.                         try {
  8.                                 Thread.sleep(1000);
  9.                         } catch (InterruptedException e) {
  10.                                 // TODO 自動產生的 catch 區塊
  11.                                 e.printStackTrace();
  12.                         }
  13.                 }
  14.                 System.out.println("時間到!"+Thread.currentThread().getName());
  15.         }
  16. }
複製代碼

TOP

返回列表