返回列表 發帖

執行緒 (二)

本帖最後由 tonyh 於 2019-11-23 13:43 編輯

以主類別繼承Thread類別,建立執行緒。

  1. public class Ch122 extends Thread {
  2.         /*
  3.         Ch122()
  4.         {
  5.                 start();       
  6.         }
  7.         */
  8.        
  9.         public void run()
  10.         {
  11.                 for(int i=5; i>=1; i--)
  12.                 {
  13.                         System.out.println(i+"秒");
  14.                         try {
  15.                                 sleep(1000);
  16.                         } catch (InterruptedException e) {}
  17.                 }
  18.                 System.out.println("時間到!");
  19.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());       
  20.         }
  21.        
  22.         public static void main(String[] args) {
  23.                 Ch122 app=new Ch122();
  24.                 app.start();
  25.                 //app.run();
  26.         }
  27. }
複製代碼

  1. public class Ch123 extends Thread {

  2.         
  3.         public void run()
  4.         {
  5.                 System.out.println("給你5秒鐘走路!!聽到沒有!!");
  6.                 for(int i=5; i>=1; i--)
  7.                 {
  8.                         System.out.println(i+"秒");
  9.                         try {
  10.                                 sleep(1000);
  11.                         } catch (InterruptedException e) {}
  12.                 }
  13.                 System.out.println("時間到了~還不快滾!");
  14.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());        
  15.         }
  16.         
  17.         public static void main(String[] args) {
  18.                 Ch123 app=new 123);
  19.                 app.start();
  20.         }
  21. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  1. public class Ch123 extends Thread{
  2.         Ch123(){
  3.                 start();
  4.         }
  5.         public void run(){for(int i=5;i>=1;i--){                       
  6.                 System.out.println(i+"seconds");
  7.                 try{
  8.                         Thread.sleep(1000);                               
  9.                 }catch(InterruptedException e){}
  10.         }
  11.         System.out.println("Time's up!");
  12.         System.out.println("Thread's name:"+Thread.currentThread().getName());
  13.     }
  14.         public static void main(String[] args){               
  15.                 Ch123 jock=new Ch123();
  16.                 jock.start();
  17.         }
  18. }
複製代碼

TOP

  1. public class Ch122 extends Thread {

  2.         
  3.         public void run()
  4.         {
  5.                 for(int i=5; i>=1; i--)
  6.                 {
  7.                         System.out.println(i+"秒");
  8.                         try {
  9.                                 sleep(1000);
  10.                         } catch (InterruptedException e) {}
  11.                 }
  12.                 System.out.println("時間到!");
  13.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());        
  14.         }
  15.         
  16.         public static void main(String[] args) {
  17.                 Ch122 app=new Ch122();
  18.                 app.start();
  19.                
  20.         }
  21. }
複製代碼

TOP

  1. public class Ch122 extends Thread {   
  2.         public void run()
  3.         {
  4.                 for(int i=5; i>=1; i--)
  5.                 {
  6.                         System.out.println(i+"秒");
  7.                         try {
  8.                                 sleep(1000);
  9.                         } catch (InterruptedException e) {}
  10.                 }
  11.                 System.out.println("時間到!");
  12.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());        
  13.         }      
  14.         public static void main(String[] args) {
  15.                 Ch122 app=new Ch122();
  16.                 app.start();
  17.         }
  18. }
複製代碼

TOP

  1. public class Ch121 {
  2.         
  3.         public static void main(String[] args) {
  4.                 for(int i=5; i>=1; i--)
  5.                 {
  6.                         System.out.println(i+"秒");
  7.                         try {
  8.                                 Thread.sleep(1000);
  9.                         } catch (InterruptedException e) {}
  10.                 }
  11.                 System.out.println("時間到!");
  12.                 System.out.println("執行緒名稱: "+Thread.currentThread().getName());
  13.         }
  14. }
複製代碼

TOP

返回列表