1.GCD
- 同步/異步和串行/并發(fā)
- dispatch_barrier_async
- dispatch_group
(1)同步/異步和串行/并發(fā)
- dispatch_sync(serial_queue, ^{//任務(wù)})讯壶;
- dispatch_async(serial_queue,^{//任務(wù)})核无;
- dispatch_sync(concurrent_queue,^{//任務(wù)});
- dispatch_async(concurrent_queue,^{//任務(wù)});
同步串行
上面代碼會產(chǎn)生- (void)viewDidLoad { [super viewDidLoad]; dispatch_sync(dispatch_get_main_queue(), ^{ [self doSomething]; }); }
死鎖
舟扎,產(chǎn)生死鎖的原因是,隊列引起的循環(huán)等待逐工。
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_sync(serialQueue, ^{
[self doSomething];
});
}
上面的代碼是沒有問題的
同步并發(fā)
- (void)viewDidLoad {
NSLog(@"1");
dispatch_sync(global_queue(), ^{
NSLog(@"2");
dispatch_sync(global_queue(), ^{
NSLog(@"3");
});
NSLog(@"4");
});
NSLog(@"5");
}
打印結(jié)果:12345
異步串行
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomething];
});
}
異步并發(fā)
- (void)viewDidLoad {
dispatch_async(global_queue,^{
NSLog(@"1");
[self performSelector:@selector(printLog)
withObject:nil
afterDelay:0];
NSLog(@"3");
})
}
- (void)printLog {
NSLog(@"2");
}
打印結(jié)果: 13
2.dispatch_barrier_async()
(1)怎樣利用GCD實現(xiàn)多讀單寫?
dispatch_barrier_async(concurrent_queue,^{//寫操作})馅精;
#import <Foundation/Foundation.h>
@interface UserCenter : NSObject
- (id)objectForKey:(NSString *)key;
- (void)setObject:(id)obj forKey:(NSString *)key;
@end
#import "UserCenter.h"
@interface UserCenter()
{
dispatch_queue_t concurrent_queue;
NSMutableDictionary *userCenterDic;
}
@end
@implementation UserCenter
- (instancetype)init{
self = [super init];
if (self) {
// 通過宏定義DISPATCH_QUEUE_CONCURRENT 創(chuàng)建一個并發(fā)隊列
concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
// 創(chuàng)建數(shù)據(jù)庫容器
userCenterDic = [NSMutableDictionary dictionary];
}
return self;
}
- (id)objectForKey:(NSString *)key{
__block id obj;
//同步讀取指定數(shù)據(jù)
dispatch_sync(concurrent_queue, ^{
obj = [userCenterDic objectForKey:key];
});
return obj;
}
- (void)setObject:(id)obj forKey:(NSString *)key{
// 異步柵欄調(diào)用設(shè)置數(shù)據(jù)
dispatch_barrier_async(concurrent_queue, ^{
[userCenterDic setObject:obj forKey:key];
});
}
@end
3.dispatch_group_async()
使用GCD實現(xiàn)這個需求:A岩馍、B、C三個任務(wù)并發(fā)伙菊,完成后執(zhí)行任務(wù)D?
#import <Foundation/Foundation.h>
@interface GroupObject : NSObject
- (void)handle;
@end
#import "GroupObject.h"
@interface GroupObject()
{
dispatch_queue_t concurrent_queue;
NSMutableArray <NSURL *> *arrayURLs;
}
@end
@implementation GroupObject
- (instancetype)init
{
self = [super init];
if (self) {
//創(chuàng)建并發(fā)隊列
concurrent_queue = dispatch_queue_create("concurrent_queue",DISPATCH_QUEUE_CONCURRENT);
arrayURLs = [NSMutableArray array];
}
return self;
}
- (void)handle{
//創(chuàng)建一個group
dispatch_group_t group = dispatch_group_create();
// for循環(huán)遍歷各個元素
for (NSURL *url in arrayURLs) {
//異步組分派到并發(fā)隊列當中
dispatch_group_async(group, concurrent_queue, ^{
//根據(jù)url去下載圖片
NSLog(@"%@",url);
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//當添加到數(shù)組中的所有任務(wù)執(zhí)行完成之后會調(diào)用該block
NSLog(@"所有圖片下載完成");
});
}
@end
4.NSOperation
需要和NSOperationQueue配合使用來實現(xiàn)多線程方案
- 添加任務(wù)依賴
- 任務(wù)執(zhí)行狀態(tài)控制
- 最大并發(fā)量
(1)任務(wù)執(zhí)行狀態(tài)控制
- isReady 當前任務(wù)是否處于就緒狀態(tài)
- isExecuting 當前任務(wù)是否處于執(zhí)行中的狀態(tài)
- isFinished 當前任務(wù)是否已經(jīng)執(zhí)行完成
- isCancelled 當前任務(wù)是否已取消
如果只重寫main方法败玉,底層控制變更任務(wù)執(zhí)行狀態(tài)敌土,以及任務(wù)退出。
如果重寫了start方法运翼,自行控制任務(wù)狀態(tài)返干。