主题:深入理解同步(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
满意答案
就像我在评论中发布的那样:我会启用剪辑
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 subviewbackgroundView
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
相关问答
更多为什么所有的背景消失在UITableViewCell选择?(Why do all backgrounds disappear on UITableViewCell select?)
选择单元格时,UITableViewCell子视图消失(UITableViewCell subview disappears when cell is selected)
在单元格的边界外面向UITableViewCell添加了UIImageView(Added UIImageView to UITableViewCell outside cell's bounds)
无法选择或访问超出滚动边界的uitableview单元格(不可见的单元格)(unable to select or access uitableview cell that is out of scroll bounds (cells that are not visible))
如何使按钮透明的UITableViewCell的背景(How to make background of UITableViewCell with buttons transparent)
UITableViewCell - 根据最后一个单元格的属性显示文本和背景图像(UITableViewCell - Text and background image are displayed based on last cell's attributes)
iOS 7 UItableview单元格背景视图(iOS 7 UItableview cell background view)
保持UITableViewCell的背景视图忽略单元格边界(Keep UITableViewCell's background view from ignoring cell bounds)
细胞背景视图不填充整个细胞(Cell background view not filling the whole cell)
阻止单元格被拖出UITableView边界(Stop cell from being dragged out of UITableView bounds)
相关文章
更多深入理解Android:卷2.pdf电子书下载
深入理解Magento -前言
深入理解Hadoop集群和网络 PDF
深入理解搜索-精确匹配搜索
Storm Topology的并发度
微信公共服务平台开发(.Net 的实现)12-------网页授权(上 :更加深入理解OAuth2.0 )
原来炒股可以这样理解
Storm-源码分析-Topology Submit-Executor-mk-threads
请问何为“同步流程”
数据同步工具 DataX 的使用
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!