Thread difference in the start and run of the method

public  class ThreadTest {
     // definition of a method 
    Private  static  void testdemo () {
         // output this to the thread name 
        System.out.println ( "Method testdemo here:" + . Thread.currentThread () getName ());
    }
    public static void main(String[] args) {
        Thread t = new Thread(){
            public void run(){
                testdemo ();
            }
        };
        System.out.println ( "This is the main method:" + Thread.currentThread () getName ().);
        t.run();
     // t.start(); } }

The results appear in the run method calls

 

 

 The results appear in the start method is called

 

 接下来我们来看下调用start这个方法为什么会不一样

点击进入start这个方法

 

jdk网站:http://hg.openjdk.java.net

 

进入到具体的目录:http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/b860bcc84d51/src/share/native/java/lang

这里看的是jdk8的

 进入Thread.c这个文件

 

 来到JVM文件:http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/8c0fa90986a6/src/share/vm/prims/jvm.cpp

搜索JVM_StartThread

 

 然后再去深入看下thread_entry这里做了什么

 

1、调用start方法的时候是会去创建一个新的子线程,但是最后还是调用了run方法;即调用start()--->start0()-->JVM_StartThread-->thread_entry-->run()

2、调用run方法的时候还在主线程调用,即t.run();-->Thread这个类中的重写的run方法

start方法最后还是调用了run方法,所以两者除了执行步骤不一样,其他都差不多

 

Guess you like

Origin www.cnblogs.com/ltzdream/p/12147650.html