首页 > 学院 > 开发设计 > 正文

[JAVA100例]062、多线程

2019-11-18 10:53:38
字体:
来源:转载
供稿:网友

  /**
 * <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!");
  }
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表