多线程的创建

1.继承Thread

2.重写Thread的run( )方法 -->将此线程执行的操作声明在run( )中

3.创建Thread类的子类的对象

4.调用此对象调用start

 

例如:

class MyThread  extends Thread {

  public void run( ){

    int i=0;

    int count = 0;//定义一个接收的变量

    while(i<=1000){

    count += i;//进行累加

     i++; }

    System.out.println("100的累加和为"+count);

  }

}

public class ThreadTest {

  public class void main(String[] args){

    3.//创建Thread类的子类的对象

    MyThread m1 = new MyThread( ) ;

    4.//调用此对象调用start

    m1.start( ); 

    while(i<=10000){

     System.out.println("114514");

       }

  }

}

posted @ 2022-11-07 21:36  平凡的柳先生  阅读(25)  评论(0)    收藏  举报