主题:深入理解同步(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?)[2021-12-26]
正在发生的事情是,TableViewCell中的每个子视图都将接收setSelected和setHighlighted方法。 setSelected方法将删除背景颜色,但如果将其设置为所选状态,则将被更正。 例如,如果这些是在自定义单元格中添加为子视图的UILabels,则可以将其添加到TableViewCell实现代码的setSelected方法中: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSel ... -
UITableViewCell在选择或突出显示单元格时更改所有子视图的背景颜色,您可以通过覆盖Tableview单元格的setSelected:animated和setHighlighted:animated来解决此问题setHighlighted:animated和重置视图背景颜色。 目标C: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { UIColor *color = self.yourView.backgroundCo ...
-
在单元格的边界外面向UITableViewCell添加了UIImageView(Added UIImageView to UITableViewCell outside cell's bounds)[2022-06-21]
“然后这个图像显示在单元格的边界之外。” 你的视图不会在它的范围之外接受触摸,因此如果父视图没有,则子视图将不会接收到触摸 "This image is then displayed outside the bounds of the cell." your view will not receive touches outside it's bounds therefore the subview will not receive touches if it's parent does not -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"genericCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; i ...
-
如何使按钮透明的UITableViewCell的背景(How to make background of UITableViewCell with buttons transparent)[2023-08-03]
得到它了。 以为我会发布它,如果任何人都处于同样的困境: UIView *backView = [[UIView alloc] initWithFrame:CGRectZero]; backView.backgroundColor = [UIColor clearColor]; cell.backgroundView = backView; [backView release]; 现在看起来像这样: Got it. Thought I'd post it if anybody else is in the ... -
我认为问题在于您使用实例变量cellHeight来存储每行的单元格高度,并且在需要时不会更新。 您可以尝试使用(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath动态获取单元格高度- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
-
尝试添加:cell.backgroundColor = [UIColor clearColor]; try adding : cell.backgroundColor = [UIColor clearColor];
-
保持UITableViewCell的背景视图忽略单元格边界(Keep UITableViewCell's background view from ignoring cell bounds)[2022-11-05]
就像我在评论中发布的那样:我会启用剪辑self因为子视图backgroundView太大了。 另外:我会用 UIImageView *imgView = [[UImageView alloc] initWithImage:] imgView.frame = CGRectMake(...) 也可以尝试机器人设置框架,因为Cell也可以自动设置backgroundView的框架 Like I posted in the comments: I would enable clipping on self bec ... -
试试这个代码。 它对我有用。 UIImageView * ac= [[UIImageView alloc] init]; ac.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]; cell.backgroundView =ac; cell.backgroundColor = [UIColor clearColor]; Try this code. it worked for me. UII ...
-
你可以像下面这样做.. [yourtableview setBounces:NO]; 让我知道它的工作与否!!!! 快乐的编码!!! you can do this like below.. [yourtableview setBounces:NO]; let me know it is working or not!!!! Happy Coding!!!