目录
创建新线程
当Java程序启动的时候,实际上是启动了一个JVM进程,然后,JVM启动主线程来执行main()
方法。在main()
方法中,又可以启动其他线程。
1. Thread类:继承Thread类
- 重写run方法;
- 在run方法里写自己想实现的代码;
- 启动线程:
创建对象Thread子类对象,调用start():
package com.fyc0714;
//继承Thread类
class TestThread extends Thread{
//重写run
public void run( ) {
System.out.println("线程01");
}
}
public class Test {
public static void main(String[] args) {
//创建对象
TestThread t = new TestThread();
//调用start方法
t.