/**
* <p>Title: 創建多線程</p>
* <p>Description: 使用構造器,創建多線程。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: multiThread.java</p>
* @version 1.0
*/
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!");
}
}
}