继承Thread类

  • 自定义线程类继承Thread类
  • 重写run()方法,编写线程执行体
  • 创建线程对象,调用start()方法启动线程
package com.thread.test2;

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

//注意:线程开启不一定立即执行,由cpu调度执行
public class TestThread1 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("我在执行代码"+i);
        }
    }


    public static void main(String[] args) {
        //创建一个线程对象
        TestThread1 testThread1 = new TestThread1();
        //调用start方法
        testThread1.start();

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

posted @ 2022-04-28 16:18  好12  阅读(26)  评论(0)    收藏  举报