通常,不可變的類(如: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/