ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线程详解

2021-01-14 17:03:43  阅读:186  来源: 互联网

标签:group started start0 详解 线程 run method


线程详解
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方法

标签:group,started,start0,详解,线程,run,method
来源: https://www.cnblogs.com/2014-1130/p/14278123.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有