安卓开启子线程常用的方法

方法一
构造函数传入Runnable,重写run方法

new Thread(new Runnable() {
            @Override
            public void run() {
            }
        }).start();

方法二
Thread实现run方法

new Thread(){
            @Override
            public void run() {
            }
        }.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".
         */
        // Android-changed: throw if 'started' is true
        if (threadStatus != 0 || started)
            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);

        started = false;
        try {
            nativeCreate(this, stackSize, daemon);
            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 */
            }
        }
    }

    private native static void nativeCreate(Thread t, long stackSize, boolean daemon);

启动线程之后,最终调用了native层的nativeCreate方法,最后native会调用Thread的run方法,此时run方法就执行在子线程了。

@Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

这时我们发现这里有一个target,查看一下Thread的构造函数

public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }
    
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
        Thread parent = currentThread();
        if (g == null) {
            g = parent.getThreadGroup();
        }

        g.addUnstarted();
        this.group = g;

        this.target = target;
        this.priority = parent.getPriority();
        this.daemon = parent.isDaemon();
        setName(name);

        init2(parent);

        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;
        tid = nextThreadID();
    }

我们发现其实这个target就是我们传入的runnable,这也就解释了为啥传入runnable也可以执行子线程。那么问题来了我们是否可以来个这样的骚操作(线程:这是人干出来的事么)?

new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("Thread",Thread.currentThread().getId()+"Runnable");
            }
        }){
            @Override
            public void run() {
            	//这一行影响能否执行Runnable里的run方法
                super.run();
                Log.e("Thread",Thread.currentThread().getId()+"run");
            }
        }.start();

经打印发现两个方法都能执行,而且是同一个子线程里。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值