创建线程的三种方式

  • 继承Thread类(重点)
  • 实现Runnable接口(重点)推荐使用
  • 实现Callable接口(了解)

继承Thread类

package Thread;

/**
 * 创建线程方式一:继承Thread
 *
 * @author ZhaoLu cang on 2021/4/10 0010
 */
//注意,线程开启不一定立即执行,由cpu调度执行
public class Thread01 extends Thread{
    //重写run方法
    @Override
    public void run() {
        //run方法线程体
        for(int i=0;i<20;i++){
            System.out.println("学习多线程中......");

        }
    }

    public static void main(String[] args) throws InterruptedException {
        //创建一个线程对象
        Thread01 thread=new Thread01();
        //调用start方法
        thread.start();
        //main主线程
        for(int i=0;i<20;i++){
            System.out.println("正在学习代码......");

        }
    }
}
View Code

相关文章: