Demo19 :使用構造器創建多線程

package demo19;

/**
 * <p>Title: 創建多線程</p>
 * <p>Description: 使用構造器,創建多線程。</p>
 */
public class multiThread 
{ 
/**
 *<br>方法說明:主方法
 *<br>輸入參數:
 *<br>返回類型:
 */
 public static void main (String [] args){ 
    new multiThread();
  }
/**
 *<br>方法說明:構造器。構造多個線程,並啓動它們。
 *<br>輸入參數:
 *<br>返回類型:
 */
  multiThread(){
    for (int i = 0; i < 5; i++){
      System.out.println("Creating thread "+i);
      innThread mt = new innThread (i); 
      mt.start (); 
    }
  }
/**
 *<br>類說明:內部類通過繼承Thread實現線程 
 *<br>類描述:通過構造器參數,區別不同的線程
 */     
 class innThread extends Thread 
 { 
  int count;
  innThread(int i){
     count=i;
  }
/**
 *<br>方法說明:內部類線程主體,繼承Thread必須實現的方法。
 *<br>輸入參數:
 *<br>返回類型:
 */
  public void run () 
  { 
    System.out.println("now "+count+" thread is beginning..... ");
    try{
      sleep(10-count);
    }catch(Exception e){
      System.out.println(e);
    }
    System.out.println("\n"+count+" thread is end!");
  } 
 } 
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章