继承Thread类
- 自定义线程类继承Thread类
- 重写run()方法,编写线程执行体
- 创建线程对象,调用start()方法启动线程
- 线程开启并不一定执行,由CPU调度
package exception.Demo03;
//创建 线程方式一、继承Thread类,重写run()方法,调用start()方法,开启线程
public class TestThread extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i <20; i++) {
System.out.println("我在看代码----"+i);
}
}
public static void main(String[] args) {
//主方法,main方法,主线程
//创建一个线程对象
TestThread testThread = new TestThread();
//调用start()方法,开启线程
testThread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("我在学习多线程----"+i);
}
}
}