通过Thread类创建线程

package day16;

public class ThreadTest extends Thread {
    @Override
    public void run() {
        //run方法线程体
        for (int i = 1; i <= 6; i++) {
            System.out.println("run方法线程:" + i);
        }
    }

    public static void main(String[] args) {
        //创建一个线程
        ThreadTest thread1 = new ThreadTest();
        //开启线程:start()
        thread1.start();

        //main主线程
        for (int i = 1; i <= 30; i++) {
            System.out.println("主线程:" + i);
        }
    }
}
运行的结果是并发执行,穿插打印,执行结果如下:

posted @ 2021-08-11 22:46  Eleanor123  阅读(95)  评论(0编辑  收藏  举报