多线程不直接使用run,而是直接使用start(),内部含有synchronized锁,有threadstatus状态,状态不为0时会抛出异常,为什么会抛出异常,因为一个线程只允许被启动一次,重复启动线程的话则会被抛出异常IlegalThreadStateException,该线程是runtimeException子类。
public synchronized void start() { if (this.threadStatus != 0) { throw new IllegalThreadStateException(); } else { this.group.add(this); boolean var1 = false; try { this.start0(); var1 = true; } finally { try { if (!var1) { this.group.threadStartFailed(this); } } catch (Throwable var8) { ; } } } }
start方法用会调用start0方法,该方法是native方法,为什么是native方法,因为start0线程需要依赖不同的操作系统的算法去运行,需要根据不同操作系统,不同的硬件标准,CPU资源,内存资源,IO资源等,无论是在window还是linux等系统,都需要在各个操作系统中去搭建各自的JVM,JVM在整个过程中准备了start0()方法,要想程序启动,程序需要在strat()找到start0()方法,JVM负责实现start0()方法,JVM需要根据不同的操作系统去实现,start0()实际上是JVM匹配操作系统,JVM实际上是找到底层函数与我们操作系统实现算法的控制,实现资源的访问。
private native void start0();
多线程的启动永远只有一个方法,Thread的start()。