创建线程方式一: 继承Thread类,重写run()方法,调用start开启线程

package com.guo.demo01;

//创建线程方式一: 继承Thread类,重写run()方法,调用start开启线程
//extends Thread
//总结:注意,线程开启不一定立即执行,由cpu调度执行

public class TestThread1 extends Thread{
    @Override
    public void run() {
        //run方法 线程体
        for (int i = 0; i < 10; i++) {
            System.out.println("我爱李悦"+i);
        }

    }
    //main方法 主线程
    public static void main(String[] args) {

        TestThread1 thread1 = new TestThread1();
        TestThread1 thread2 = new TestThread1();
        thread1.start();//多线程  同时执行  cpu调度,每次结果不一样
        thread2.start();//多线程  同时执行  cpu调度,每次结果不一样
        //thread1.run();//先执行run方法
        for (int i = 0; i < 10; i++) {
            System.out.println("我更爱李悦"+i);
        }

    }
}

posted @ 2026-03-23 10:41  果子同志  阅读(9)  评论(0)    收藏  举报