GCD
- (void)viewDidLoad {
[super viewDidLoad];
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// NSLog(@"%@", [NSThread currentThread]);
// });
//
// dispatch_apply(4, DISPATCH_QUEUE_PRIORITY_DEFAULT, ^(size_t index) {
// NSLog(@"%zu", index);
// });
// Do any additional setup after loading the view, typically from a nib.
//GCD异步线程的创建
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//GCD 重复执行线程
dispatch_apply(4, DISPATCH_QUEUE_PRIORITY_DEFAULT, ^(size_t index) {
});
//GCD 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("com.changtu.be", DISPATCH_QUEUE_CONCURRENT);
//线程ABC 一次执行.因为它是一个串行队列
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1];
});
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:3];
});
dispatch_async(queue, ^{
});
// GCD群组通知等所有线程都完成工作后.在做通知
dispatch_group_t group = dispatch_group_create();
//线程AB
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//待群组里的线程都完成之后调用的通知
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//GCD 实现计时器
__block int time = 30;
CGFloat reSecond = 1.0;
dispatch_queue_t queueTime = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queueTime);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, reSecond * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
time--;
NSLog(@"%d", time);
if (time == 0) {
dispatch_source_cancel(timer);
}
});
dispatch_resume(timer);
// GCD线程间通信-调用主线程修改UI:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"异步数据处理 - %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
NSLog(@"数据处理完成");
// 调用主线程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"更新UI - %@", [NSThread currentThread]);
// [self editUINow];
});
});
// 获取主线程
dispatch_get_main_queue();
// 创建队列:第一个参数是队列的名称,它会出现在调试程序等之中,是个内部名称。第二个参数代表它是串行队列还是并并发队列,NULL代表串行队列。
dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
// 创建异步调度队列
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
// 恢复队列
void dispatch_resume(dispatch_object_t object);
// 暂停队列
void dispatch_suspend(dispatch_object_t object);
[super viewDidLoad];
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// NSLog(@"%@", [NSThread currentThread]);
// });
//
// dispatch_apply(4, DISPATCH_QUEUE_PRIORITY_DEFAULT, ^(size_t index) {
// NSLog(@"%zu", index);
// });
// Do any additional setup after loading the view, typically from a nib.
//GCD异步线程的创建
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//GCD 重复执行线程
dispatch_apply(4, DISPATCH_QUEUE_PRIORITY_DEFAULT, ^(size_t index) {
});
//GCD 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("com.changtu.be", DISPATCH_QUEUE_CONCURRENT);
//线程ABC 一次执行.因为它是一个串行队列
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1];
});
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:3];
});
dispatch_async(queue, ^{
});
// GCD群组通知等所有线程都完成工作后.在做通知
dispatch_group_t group = dispatch_group_create();
//线程AB
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//待群组里的线程都完成之后调用的通知
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//GCD 实现计时器
__block int time = 30;
CGFloat reSecond = 1.0;
dispatch_queue_t queueTime = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queueTime);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, reSecond * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
time--;
NSLog(@"%d", time);
if (time == 0) {
dispatch_source_cancel(timer);
}
});
dispatch_resume(timer);
// GCD线程间通信-调用主线程修改UI:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"异步数据处理 - %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
NSLog(@"数据处理完成");
// 调用主线程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"更新UI - %@", [NSThread currentThread]);
// [self editUINow];
});
});
// 获取主线程
dispatch_get_main_queue();
// 创建队列:第一个参数是队列的名称,它会出现在调试程序等之中,是个内部名称。第二个参数代表它是串行队列还是并并发队列,NULL代表串行队列。
dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
// 创建异步调度队列
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
// 恢复队列
void dispatch_resume(dispatch_object_t object);
// 暂停队列
void dispatch_suspend(dispatch_object_t object);
}
posted on 2015-12-04 17:01 taiyang2014 阅读(259) 评论(0) 编辑 收藏 举报