zoukankan      html  css  js  c++  java
  • 线程详解

    线程详解
    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(你不知道一样东西,你也会不知道自己不知道这样东西)
  • 相关阅读:
    移动端UI
    jQuery 下拉框三级联动
    jQuery基础与学习资源
    jQuery
    TCP、UDP、HTTP、SOCKET之间的区别
    Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
    android 中 任务、进程和线程的区别
    android中不同手机分辨率适配问题
    经验分享:CSS浮动(float,clear)通俗讲解(真的很通俗)
    关于各种排列组合java算法
  • 原文地址:https://www.cnblogs.com/2014-1130/p/14278123.html
Copyright © 2011-2022 走看看