进阶day05-多线程启动方式一:继承Thread类

This commit is contained in:
2026-03-07 10:51:49 +08:00
parent 03131a044d
commit 165ed9b918
2 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.inmind.thread07;
public class MyThread extends Thread{
//默认无参构造方法
/*public MyThread(){
super();
}*/
@Override
public void run() {//重写的run方法的作用当前线程要执行的任务
for (int i = 0; i < 1000; i++) {
System.out.println("hello pyhton");
}
}
}

View File

@@ -0,0 +1,23 @@
package com.inmind.thread07;
/*
在java中使用一个类Thread来表示一个线程.
启动多线程的方式一:
1.定义一个子类继承Thread
2.重写run方法
3.创建子类对象,调用start方法启动
*/
public class ThreadDemo02 {
public static void main(String[] args) {
//开启一个新的线程执行
MyThread myThread = new MyThread();
// myThread.run();//不能直接调用run方法否则不启动多线程
//注意start方法的作用是真的启动线程启动起来的新线程的任务是执行该线程类的run方法
myThread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("hello java");
}
System.out.println("程序结束");
}
}