返回列表 發帖

執行緒 (四) - 多執行緒

本帖最後由 tonyh 於 2019-11-23 14:21 編輯

自訂多個類別,繼承Thread類別,建立執行緒。

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

  17. class MyThread1 extends Thread
  18. {
  19.         public void run()
  20.         {
  21.                 for(int i=5; i>=1; i--)
  22.                 {
  23.                         System.out.println(i+"秒  "+Thread.currentThread().getName());
  24.                         try {
  25.                                 sleep(1000);
  26.                         } catch (InterruptedException e) {}
  27.                 }
  28.                 System.out.println("時間到! "+Thread.currentThread().getName());
  29.         }
  30. }

  31. class MyThread2 extends 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.                                 sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("時間到! "+Thread.currentThread().getName());
  43.         }
  44. }
複製代碼

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

  17. class MyThread1 extends Thread
  18. {
  19.         public void run()
  20.         {
  21.                 for(int i=5; i>=1; i--)
  22.                 {
  23.                         System.out.println(i+"seconds  "+Thread.currentThread().getName());
  24.                         try {
  25.                                 sleep(1000);
  26.                         } catch (InterruptedException e) {}
  27.                 }
  28.                 System.out.println("time's up "+Thread.currentThread().getName());
  29.         }
  30. }

  31. class MyThread2 extends Thread
  32. {
  33.         public void run()
  34.         {
  35.                 for(int i=1; i<=5; i++)
  36.                 {
  37.                         System.out.println(i+"seconds  "+Thread.currentThread().getName());
  38.                         try {
  39.                                 sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("time's up "+Thread.currentThread().getName());
  43.         }
  44. }
複製代碼

TOP

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

  17. class MyThread1 extends Thread
  18. {
  19.         public void run()
  20.         {
  21.                 for(int i=5; i>=1; i--)
  22.                 {
  23.                         System.out.println(i+"秒  "+Thread.currentThread().getName());
  24.                         try {
  25.                                 sleep(1000);
  26.                         } catch (InterruptedException e) {}
  27.                 }
  28.                 System.out.println("時間到! "+Thread.currentThread().getName());
  29.         }
  30. }

  31. class MyThread2 extends 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.                                 sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("時間到! "+Thread.currentThread().getName());
  43.         }
  44. }
複製代碼

TOP

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

  17. class MyThread1 extends Thread
  18. {
  19.         public void run()
  20.         {
  21.                 for(int i=5; i>=1; i--)
  22.                 {
  23.                         System.out.println(i+"秒  "+Thread.currentThread().getName());
  24.                         try {
  25.                                 sleep(1000);
  26.                         } catch (InterruptedException e) {}
  27.                 }
  28.                 System.out.println("時間到! "+Thread.currentThread().getName());
  29.         }
  30. }

  31. class MyThread2 extends 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.                                 sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("時間到! "+Thread.currentThread().getName());
  43.         }
  44. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

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

  17. class MyThread1 extends Thread
  18. {
  19.         public void run()
  20.         {
  21.                 for(int i=5; i>=1; i--)
  22.                 {
  23.                         System.out.println(i+"秒  "+Thread.currentThread().getName());
  24.                         try {
  25.                                 sleep(1000);
  26.                         } catch (InterruptedException e) {}
  27.                 }
  28.                 System.out.println("時間到! "+Thread.currentThread().getName());
  29.         }
  30. }

  31. class MyThread2 extends 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.                                 sleep(1000);
  40.                         } catch (InterruptedException e) {}
  41.                 }
  42.                 System.out.println("時間到! "+Thread.currentThread().getName());
  43.         }
  44. }
複製代碼

TOP

返回列表