线程详解

线程详解
1.我们都知道一个线程的启动,只有两种方式第一种是继承Thread方法,重写run方法,第二种是实现Runnable,叫个Thread执行
2. 通过代码我们可以看到线程启动方法中,有start0(), 真正去启动一个线程,发现这个方法只是native修饰的方法,这边拓展一下什么是native修饰的方法

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 */
            }
        }
    }

拓展:Native Method,查阅英文文献的解释,"A native method is a Java method whose implementation is provided by non-java code.",准确的说非java实现的方法,主要为了加载文件和动态链接库,可以直接操作系统属性
3. 通过jni实现start0,内部初始化线程,最终会执行,vmSymbols::run_method_name():即"run";,也是java线程中run方法

线程的生命周期:初始化(new) --> 可运行状态(start) --> 运行状态(run)& 子状态暂停和阻塞 --> 销亡

具体业务:异步操作,FutureTask,里面有实现Runnable方法

Unkonw Unkonw(你不知道一样东西,你也会不知道自己不知道这样东西)
原文地址:https://www.cnblogs.com/2014-1130/p/14278123.html