首页 \ 问答 \ 主题:深入理解同步(Threads: deep understanding of synchronizing)

主题:深入理解同步(Threads: deep understanding of synchronizing)

我只是想在同步时更深入地了解内在机制。 我准备了3个例子。 我有问题涉及他们每个人。 所以这是第一个例子:


public class SyncExamples 
{

    SyncClass sync1, sync2;


    public void execute1()
    {
        sync1 = new SyncClass();
        sync1.process();
    }


    public void execute2()
    {
        sync2 = new SyncClass();
        sync2.process();
    }



    class SyncClass
    {

        public synchronized void process()
        {

        }
    }
}

SyncClass的方法process()是同步的。 但由于在SyncExamples类中创建了两个不同的SyncClass 对象 ,因此它们可以同时执行,而不是它们。 它们引用不同的对象,因此没有任何同步。 这样对吗?

第二个例子:


public class SyncExamples 
{

    SyncClass sync1 = new SyncClass();


    public void execute1()
    {       
        sync1.process();
    }


    public void execute2()
    {       
        sync1.process();
    }




    class SyncClass
    {

        public synchronized void process()
        {

        }
    }
}

所以在这个例子中他们引用了同一个对象。 所以这里我们有一个互斥量。 他们是同步的。 但让我们来看看对我来说最有趣的例子。


public class SyncExamples 
{

    SyncClass sync1 = new SyncClass();
    ReadWriteLock lock = new ReentrantReadWriteLock(); 


    public void execute1()
    {       
        lock.writeLock().lock();
        sync1.process();
        lock.writeLock().unlock();
    }


    public void execute2()
    {       
        execute1();
    }


    public void execute3()
    {       
        sync1.process();
    }


    public void execute4()
    {       
        execute1();
    }


    class SyncClass
    {

        public void process()
        {

        }
    }
}

execute2()启动execute1()。 execute1()锁定sync1.process()。 因此,execute4()必须等到execute1()解锁sync1.process()。 但是execute3()怎么样? 它没有引用execute1(),而是直接调用sync1.process()而没有任何锁定。 所以execute1()设置的锁对execute3()无效? 是对的吗? 锁只对那些引用execute1()的调用有效,因为这个方法定义了一个锁?

我在一天后添加了以下示例:


public class SyncExamples 
{

    List list = new ArrayList(); 


    public void processList1()
    {       
        synchronized(list)
        {
        }
    }


    public void processList2()
    {       
        synchronized(list)
        {
        }
    }


    public void execute3()
    {       
        processList1();
    }


    public void execute4()
    {       
        processList2();
    }
}

我想澄清最后一个例子。 现在我有一个我要同步的列表。 方法processList1()同步list ...方法processList2()也是如此。 但它们可以同时执行吗? 同步是否全局锁定列表(我的意思是来自其他方法的所有其他访问)或仅与特定方法结合使用? 我仍然不明白在这个例子中是否可以同时执行execute3()和execute4(),因为它们引用了不同的方法。 同步可防止第二次访问其块。 但是有几种方法想要访问列表并且他们使用自己的同步块。 因此,如果processList1()锁定列表,那么此列表是否会为processList2()锁定? 或者这个锁对processList2()无效,因为它是一个不同的方法?


I just want to get a deeper understanding of the inherent mechanisms while synchronizing. I prepared 3 examples. And I have questions that refer to each of them. So here is the first example:


public class SyncExamples 
{

    SyncClass sync1, sync2;


    public void execute1()
    {
        sync1 = new SyncClass();
        sync1.process();
    }


    public void execute2()
    {
        sync2 = new SyncClass();
        sync2.process();
    }



    class SyncClass
    {

        public synchronized void process()
        {

        }
    }
}

The method process() of SyncClass is synchronized. But due to the fact that in the class SyncExamples two different objects of SyncClass are created they both can be executed concurrently, can't they. They refer to different objects so there isn't any synchronization. Is it right?

The second example:


public class SyncExamples 
{

    SyncClass sync1 = new SyncClass();


    public void execute1()
    {       
        sync1.process();
    }


    public void execute2()
    {       
        sync1.process();
    }




    class SyncClass
    {

        public synchronized void process()
        {

        }
    }
}

So in this example they refer to the very same object. So here we have a mutex. They are synchronized. But let's come to the example most interesting for me.


public class SyncExamples 
{

    SyncClass sync1 = new SyncClass();
    ReadWriteLock lock = new ReentrantReadWriteLock(); 


    public void execute1()
    {       
        lock.writeLock().lock();
        sync1.process();
        lock.writeLock().unlock();
    }


    public void execute2()
    {       
        execute1();
    }


    public void execute3()
    {       
        sync1.process();
    }


    public void execute4()
    {       
        execute1();
    }


    class SyncClass
    {

        public void process()
        {

        }
    }
}

execute2() starts execute1(). execute1() locks sync1.process(). For this reason execute4() has to wait until sync1.process() is unlocked by execute1(). But what about execute3()? It does not refer to execute1() but calls directly sync1.process() without any lock. So the lock set by execute1() is not valid for execute3()? Is that right? The lock is only valid for those calls that refer to execute1() as this method defines a lock?

the following example I added one day later:


public class SyncExamples 
{

    List list = new ArrayList(); 


    public void processList1()
    {       
        synchronized(list)
        {
        }
    }


    public void processList2()
    {       
        synchronized(list)
        {
        }
    }


    public void execute3()
    {       
        processList1();
    }


    public void execute4()
    {       
        processList2();
    }
}

I would like to clarify this one last example. Now I have a list that I want to synchronize. Method processList1() synchronizes the list... method processList2() does it as well. But can they be executed concurrently? Does synchronized lock the list globally (I mean for all other accesses from other methods) or only in conjunction with the specific method? I still don't understand if execute3() and execute4() can be executed concurrently in this example as they refer to different methods. Synchronized prevents a second access to its block. But there are several methods that want to get access to the list and they use their own synchronized blocks. So if processList1() locks the list, is this list then locked for processList2()? Or is this lock not valid for processList2() as it is a different method?


原文:https://stackoverflow.com/questions/20668721
更新时间:2023-07-17 19:07

最满意答案

就像我在评论中发布的那样:我会启用剪辑self因为子视图backgroundView太大了。

另外:我会用

UIImageView *imgView = [[UImageView alloc] initWithImage:]
imgView.frame = CGRectMake(...)

也可以尝试机器人设置框架,因为Cell也可以自动设置backgroundView的框架


Like I posted in the comments: I would enable clipping on self because the subview backgroundView is too large.

In addition: I would use

UIImageView *imgView = [[UImageView alloc] initWithImage:]
imgView.frame = CGRectMake(...)

also try bot setting the frame, because maybe the Cell also sets the frame automatically of the backgroundView

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)