@synchronized()采用的参数是什么 [英] What is the parameter that @synchronized() takes

查看:177
本文介绍了@synchronized()采用的参数是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道@synchronized()的作用,但是...

有时我们有:

I know what @synchronized() does, but...
sometimes we have:

1- @synchronized(self)

2- @synchronized([MyClass class])

3- @synchrinized(myObj)

1- @synchronized(self)
2- @synchronized([MyClass class])
3- @synchrinized(myObj)

什么是差异,我应该传递给这个块的参数是什么?

What is the difference, and what is the parameter I should pass to this block ?

推荐答案

来自文档


传递给的对象@synchronized 指令是用于区分受保护块的唯一
标识符。如果你在两个不同的线程中执行
前面的方法,在每个线程上为 anObj 参数传递一个不同的对象
,每个都会锁定它
继续处理而不被另一方阻止。但是,如果在两种情况下都传递
相同的对象,其中一个线程将
首先获取锁定,另一个线程将阻塞直到第一个
线程完成关键部分。

The object passed to the @synchronized directive is a unique identifier used to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for the anObj parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.

所以这取决于你想要保护什么不同时执行,
并且有三种情况的应用程序。

So it depends on what you want to protect from being executed simultaneously, and there are applications for all three cases.

例如,在

-(void)addToMyArray1:(id)obj
{
    @synchronized(self) {
        [self.myArray1 addObject:obj];
    }
}

-(void)addToMyArray2:(id)obj
{
    @synchronized(self) {
        [self.myArray2 addObject:obj];
    }
}

@synchronized 实例( self )上调用
方法的code>块不能同时执行,从而保护从不同的线程同时访问
数组。

both @synchronized blocks cannot be executed simultaneously by two threads calling the method on the same instance (self), thus protecting simultaneous access to the arrays from different threads.

但是它也阻止第一个方法的块
与块同时执行从第二种方法,因为他们
使用相同的锁 self 。因此,对于更精细的锁定,您可以使用
不同的锁:

But it also prevents the block from the first method to be executed simultaneously executed with the block from the second method, because they use the same lock self. Therefore, for more fine-grained locking, you could use different locks:

-(void)addToMyArray1:(id)obj
{
    @synchronized(self.myArray1) {
        [self.myArray1 addObject:obj];
    }
}

-(void)addToMyArray2:(id)obj
{
    @synchronized(self.myArray2) {
        [self.myArray2 addObject:obj];
    }
}

现在可以同时访问来自不同线程的self.myArray1 self.myArray2
仍然受到保护,但彼此独立。

Now the simultaneous access to self.myArray1 and self.myArray2 from different threads is still protected, but independently of each other.

上的锁可用于保护对全局变量的访问。
这只是用于演示目的的一个简单示例:

A lock on the class can be used to protect access to a global variable. This is just a trivial example for demonstration purposes:

static int numberOfInstances = 0;

-(id)init
{
    self = [super init];
    if (self) {
        @synchronized([self class]) {
            numberOfInstances++;
        }
    }
}

这篇关于@synchronized()采用的参数是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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