java多线程快速入门(二)

通过继承Thread类来实行多线程

package com.cppdy;

//通过继承Thread类来实行多线程

class MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 30; i++) {
            System.out.println("线程打印:"+i);
        }
    }
}

public class ThreadDemo {
    
    public static void main(String[] args) {
        System.out.println("主函数启动");
        MyThread mt=new MyThread();
        //mt.run();
        mt.start();
        for (int i = 0; i < 30; i++) {
            System.out.println("主函数打印:"+i);
        }
        System.out.println("主函数结束");
    }

}

注:

  mt.run();  相当于调用MyThread类的run方法

  mt.start();  开启一个线程

原文地址:https://www.cnblogs.com/jiefu/p/10013801.html