在同步块内执行新线程 [英] Execution of new thread inside a synchronized block

查看:91
本文介绍了在同步块内执行新线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在同步块中创建一个新线程,该块是否会保持锁定状态,直到线程执行完成为止?
如果没有,那么它何时会被锁定?

If i create a new thread inside a synchronized block, will the block remain locked till the thread execution is also complete? If not, then till when would it remain locked?

String sLine;
onClick(String line){
    synchronized (lock) {
        sLine = line;
        new Thread(new Runnable() {
            @Override
            public void run() {
                 doProcessing(Sline);    
        }).start(); 
    }
}


推荐答案

它只有代码 <$才会被锁定c $ c>使用新创建的线程加入() d,等待它完成。由于没有 join(),锁定将在 start()的调用完成后释放。

It would only remained locked if the code join()d with the newly created thread, thus waiting for it to finish. As there is no join() the lock will be released after the call to start() has completed.

这篇关于在同步块内执行新线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆