服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

[java100例]062、多线程


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

扫描关注微信公众号