java多线程的start和run方法的区别
代码MyRunnable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package thread;
public class MyRunnable implements Runnable{
private int i;
public MyRunnable(int i){
super();
this.i = i;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("MyRunnable is running...");
}
}
代码MyThreadTest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package thread;
public class MyThreadTest {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable(1);
//thread0.run();
Thread thread1 = new Thread(runnable);
thread1.start();
System.out.println("main is running...");
}
}
运行MyThreadTest返回结果如下:
1
2
main is running...
MyRunnable is running...
Thread运行的原理图

- Thread调用start方法,之后会调用start0(),start0()是native方法,调用JVM中JVM_startThread,之后调用vmSymbolHandles的run_method_name,最后回回调Thread中的run方法。
-
调用顺序如下
1 2 3 4 5 6
>MainThread >>Thread.start() >>>Thread.start0() >>>>JVM中JVM_startThread() >>>>>vmSymbolHandles的run_method_name >>>>>>Thread.run()
版权声明:自由转载-非商用-非衍生-保持署名 lonpo
本文永久链接:
http://lonpo.github.io/java/java-thread-start-run-method/