创建线程的方法

package com.daxing.demo01;
//创建线程的方式一:继承Thread类,重写run()方法,调用start方法开启线程
public class TestThread1  extends Thread{//继承Thread
    //重写run方法
    @Override
    public void run() {
       //这个地方是run方法线程体
        for (int i = 0; i < 20; i++) {
            System.out.println("我在看代码---"+i);
        }
    }

    public static void main(String[] args) {
        //这个地方是main线程,主线程

        //创建一个线程对象
        TestThread1 testThread1 = new TestThread1();
        //调用start()方法开启线程
        testThread1.start();
//        与testThread1。run();不一样,run()先执行完才会执行main,
        //而start是和main交替执行的,

        for (int i = 0; i < 20; i++) {
            System.out.println("我在学习多线程--"+i);
        }
    }


}

 

posted @ 2022-03-03 21:06  狂神大鑫  阅读(51)  评论(0)    收藏  举报