下面看一个示例。
- 编写一个类,通过 run 方法调用线程 ```java package thread;
public class ThreadTest { private static void attack() { System.out.println(“Fight”); System.out.println(“Current Thread is: “ + Thread.currentThread().getName()); }
public static void main(String[] args) {Thread t = new Thread() {@Overridepublic void run() {attack();}};System.out.println("current main thread is: " + Thread.currentThread().getName());t.run();}
}

2. 编写一个类,通过 start 方法调用线程
```java
package thread;
public class ThreadTest {
private static void attack() {
System.out.println("Fight");
System.out.println("Current Thread is: " + Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
attack();
}
};
System.out.println("current main thread is: " + Thread.currentThread().getName());
t.start();
}
}

调用 run 会继续使用主线程来执行方法,而用 start 的时候则会用一个非 main 的线程来执行方法。
下面看一下 start 的源码。
进入到 start0 方法
发现其是一个 native 方法
start0 调用的是 JVM_StartThread 方法

通过这里我们发现这里会创建新的线程
这里会告诉虚拟机执行 run 方法里的内容
start 方法会调用 JVM 的 StartThread 方法来创建新的子线程,并通过 thread_entry 方法去调用 run 方法
- 调用 start() 方法会创建一个新的子线程并启动
- run() 方法只是 Thread 的一个普通方法的调用
