GCD Demo
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.//同步 和 异步
//具备开辟线程的能力//GCD 栈
//队列 和 任务//A B
//串行队列
//dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)//并发队列
//dispatch_get_global_queue(long identifier, unsigned long flags)//主队列
//dispatch_get_main_queue()//同步任务 基本不适用 不会创建线程
//dispatch_sync(dispatch_queue_t queue, <#^(void)block#>)//异步任务
//dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)
if 0
//同步串行
dispatch_queue_t queue = dispatch_queue_create(“abc”, nil);//A
dispatch_sync(queue, ^{
NSLog(@”111%@”,[NSThread currentThread]);
});//B
dispatch_sync(queue, ^{
NSLog(@”222%@”,[NSThread currentThread]);
});//同步并发
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//A
dispatch_sync(queue, ^{
NSLog(@”111%@”,[NSThread currentThread]);
});//B
dispatch_sync(queue, ^{
NSLog(@”222%@”,[NSThread currentThread]);
});
endif
//异步串行 创建一个线程 执行多个任务
dispatch_queue_t queue1 = dispatch_queue_create("abc", nil);
dispatch_async(queue1, ^{
NSInteger i = 0;
while (i < 100) {
NSLog(@"%ld",i++);
}
NSLog(@"111%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
NSInteger i = 999;
while (i < 2000) {
NSLog(@"%ld",i++);
}
NSLog(@"222%@",[NSThread currentThread]);
});
//异步并发 创建多个线程 执行多个任务
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSInteger i = 0;
while (i < 100) {
NSLog(@"%ld",i++);
}
NSLog(@"111%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSInteger i = 999;
while (i < 2000) {
NSLog(@"%ld",i++);
}
NSLog(@"222%@",[NSThread currentThread]);
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//单例
- (IBAction)clcik:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3200710946,482481474&fm=80“]];
//回到主线程 刷新ui
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@”%d”,[NSThread isMainThread]);
self.imageView.image = [UIImage imageWithData:data];
});
});
}
写一个单例
+(TQPerson *)sharedPerson
{
static TQPerson *p = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
p = [[TQPerson alloc] init];
});
return p;
}
GCD Group
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if 0
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
dispatch_group_t group = dispatch_group_create();
//AF
//A 4
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@”A 任务完成”);
// });
sleep(4);
NSLog(@”A 任务完成”);
});
//B 6
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@”B 任务完成”);
// });
sleep(6);
NSLog(@”B 任务完成”);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
//
//dispatch_group_enter(dispatch_group_t group) //retain +1
//dispatch_group_leave(dispatch_group_t group) //release -1
//0
endif
[self testGroup];
}
-(void)testGroup
{
dispatch_group_t group = dispatch_group_create();
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
dispatch_group_enter(group); //+1
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_group_leave(group); //-1
NSLog(@"123");
});
dispatch_group_enter(group);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_group_leave(group); //-1
NSLog(@"456");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
//AF
}