龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

[JAVA100例]062、多线程

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
/** * pTitle: 创建多线程/p * pDescription: 使用构造器,创建多线程。/p * pCopyright: Copyright (c) 2003/p * pFilename: multiThread.Java/p * @version 1.0 */ public class multiThread { /** *br方法说明:主方法 *br输入参

  /**
   * <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(" "+count+" thread is end!");
    }
   }
  }
  
精彩图集

赞助商链接