通常,不可变的类(如:NSArray)是线程安全的,而他们对应的可变的类(如:NSMutableArray)则是线程不安全的。
@synchronized(key) {
// thread-safe code goes here
}
使用@synchronized 可以保证此类的线程安全,其实现原理是:@synchronized会先暂停一个线程A,暂停期间允许另一个线程B访问,当B线程执行完成后A线程才会执行。(这是只有两个线程的时候,如果是多个线程,则由@synchronized修饰的代码块执行期间是只有一个线程在跑,其他线程都暂停)。
例子:
@property(nonatomic, strong) NSMutableArray *mutableArray;
- (void)viewDidLoad {
[super viewDidLoad];
self.mutableArray = [NSMutableArray arrayWithCapacity:5];
for (int i = 0; i < 5; i ++) {
[self.mutableArray addObject:[NSString stringWithFormat:@"object-%i", i]];
}
[self test];
}
- (void)updateMutableArray:(NSString *)value {
for (int i = 0; i < self.mutableArray.count; i ++) {
NSString *currentObject = [self.mutableArray objectAtIndex:i];
[self.mutableArray replaceObjectAtIndex:i withObject:[currentObject stringByAppendingFormat:@"-%@", value]];
NSLog(@"%@", [self.mutableArray objectAtIndex:i]);
}
}
- (void)test {
NSString *foo = @"foo";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self updateMutableArray:foo];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self updateMutableArray:foo];
});
}
这种情况下打印:
修改 updateMutableArray:方法,使用@synchronized
- (void)updateMutableArray:(NSString *)value {
@synchronized (value) {
for (int i = 0; i < self.mutableArray.count; i ++) {
NSString *currentObject = [self.mutableArray objectAtIndex:i];
[self.mutableArray replaceObjectAtIndex:i withObject:[currentObject stringByAppendingFormat:@"-%@", value]];
NSLog(@"%@", [self.mutableArray objectAtIndex:i]);
}
}
}
再次运行,此时打印:
还有一种情况,把test修改为:
- (void)test {
NSString *foo = @"foo";
NSString *faa = @"faa";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self updateMutableArray:foo];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self updateMutableArray:faa];
});
}
这里我们需要把value修改为self.mutableArray
@synchronized (value) {
....
}
@synchronized (self.mutableArray) {
....
}
注:文章主要是翻译自: http://refactr.com/blog/2012/10/ios-tips-synchronized/