//1、GCD 继承自C语言 优点 简单方便
//开启一个子线程处理耗时的操作
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//在主线程处理UI更新相关的操作
dispatch_async(dispatch_get_main_queue(), ^{
});
});
//延时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
});
//确保程序只会运行一次,类似于锁
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
//确保程序只会运行一次
@synchronized (self) {
}
// 2、NSOperation NSOperationQueue 是对GCD更高一层的封装 可以添加依赖和设置优先级更好的控制和管理线程的并发操作 缺点:操作比GCD稍慢
NSBlockOperation *block=[NSBlockOperation blockOperationWithBlock:^{
}];
NSInvocationOperation *invo=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnOperate) object:nil];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//添加依赖,设置操作
[block addDependency:invo];
//设置操作优先级
block.queuePriority=NSOperationQueuePriorityLow;
invo.queuePriority=NSOperationQueuePriorityHigh;
[queue addOperation:block];
[queue addOperation:invo];
//3、NSThread 是一种轻量级的 需要管理线程的生命周期和并发操作
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(addBtnTypeCss:) object:nil];
//线程的开启
[thread start];
//线程的取消
[thread cancel];
//等到某个日期
[NSThread sleepUntilDate:[NSDate date]];
//设置等待多久唤起
[NSThread sleepForTimeInterval:1];