当前位置: 移动技术网 > IT编程>开发语言>Java > Java线程Run和Start的区别

Java线程Run和Start的区别

2019年02月17日  | 移动技术网IT编程  | 我要评论

先上结论:run只是thread里面的一个普通方法,start是启动线程的方法。何以见得呢?可以执行下面的代码看看run和start的区别:

package com.basic.thread;

/**
 * @author zhangxingrui
 * @create 2019-02-16 20:12
 **/
public class testrunandstart {

    private static void sayhello(){
        system.out.println("hello, world");
    }

    public static void main(string[] args) {
        thread thread = new thread(new runnable() {
            @override
            public void run() {
                sayhello();
                system.out.println("current thread: " + thread.currentthread().getname());
            }
        });

        thread.run();
        thread.start();
    }

}

  执行结果:

  由此可以看到子线程是由start来启动的,里面调用了run,所以打印出来的是子线程的name。

  另外也可以从start方法的底层代码看到,首先进入start方法里面

public synchronized void start() {
        /**
         * this method is not invoked for the main method thread or "system"
         * group threads created/set up by the vm. any new functionality added
         * to this method in the future may have to also be added to the vm.
         *
         * a zero status value corresponds to state "new".
         */
        if (threadstatus != 0)
            throw new illegalthreadstateexception();

        /* notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadstartfailed(this);
                }
            } catch (throwable ignore) {
                /* do nothing. if start0 threw a throwable then
                  it will be passed up the call stack */
            }
        }
    }

  里面调用了start0,继续跟进

private native void start0();

  哦,这里就调了native方法了,再往下我们就看不到了。但是可以去openjdk的源码里面去看一下,我用的是jdk8。

  去openjdk看源码

  https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/f0b93fbd8cf8/src/share/native/java/lang/thread.c

  

  这里调用了jvm_startthread,在jvm.h里面

  继续进入

  

  

  重点看最下面的语句native_thread = new javathread(&thread_entry, sz),好,new javathread的时候传入了thread_entry,我们再去

看一下啊thread_entry是什么:

  

  在thread_entry这个函数里面调用call_virtual方法,重点是它传入的参数run_method_name,从这里我们就知道了,噢,

原来start最终会在新线程里面调用run方法。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网