- (void)viewDidLoad {
[super viewDidLoad];
// [self groupAction_wait];
// [self groupAction_notify];
// [self barrierAction];
延迟方法
[self afterAction];
开辟新线程
[self newThreadAct];
执行一次的操作
[self onceAction];
}
-(void)groupAction_wait{
//1.队列
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"--------1");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"-------2");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"--------3");
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"-------4");
}
-(void)groupAction_notify{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//先执行---async,再执行----notify
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"---1111");
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"----222");
});
dispatch_group_notify(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"----333");
});
dispatch_group_notify(group, queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"---444");
});
}
-(void)barrierAction{
dispatch_queue_t queue = dispatch_queue_create("12345", DISPATCH_QUEUE_CONCURRENT);
//先执行---async,再执行----barrier(barrier和设置的Interval无关,按顺序执行)
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"---1");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"---4");
});
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"---2");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"---8");
});
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"----12");
});
NSLog(@"four");
}
-(void)afterAction{
//延迟方法一
[self performSelector:@selector(action) withObject:self afterDelay:4];
//不是延迟方法
[self performSelectorOnMainThread:@selector(aaaction) withObject:self waitUntilDone:YES];
//延迟方法二
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"我是小范范呀");
});
}
-(void)aaaction{
NSLog(@"performOnMainThread");
}
-(void)action{
NSLog(@"performSelector");
}
-(void)newThreadAct{
// 开辟新线程
// 将所有卡顿,消耗性能的操作全部丢到新线程中
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"例如读取数据的操作。。。。");
// 不要在新线程中去刷新UI
// 而是要在主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"给cell中的一些控件赋值");
});
});
}
-(void)onceAction{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只执行一次的操作放这里");
});
}