dispatch_sync(dispatch_queue_tqueue, dispatch_block_tblock);
dispatch_async(dispatch_queue_tqueue, dispatch_block_tblock);
dispatch_queue_t dispatch_get_global_queue(
dispatch_queue_priority_tpriority,// 队列的优先级
unsigned longflags);// 此参数暂时无用,用0即可
dispatch_queue_tqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //获得全局并发队列
dispatch_queue_t
dispatch_queue_create(const char*label, //队列名称
dispatch_queue_attr_t attr); //队列属性,一般用NULL即可
dispatch_queue_tqueue = dispatch_queue_create("cn.itcast.queue", NULL); // 创建
dispatch_release(queue); // 非ARC需要释放手动创建的队列
dispatch_queue_tqueue = dispatch_get_main_queue();
3.各种队列执行的效果注意
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//执行耗时的异步操作...
dispatch_async(dispatch_get_main_queue(),^{
// 回到主线程,执行UI刷新操作
});
});
[self performSelector:@selector(run)withObject:nil afterDelay:2.0];
// 2秒后再调用self的run方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
//2秒后执行这里的代码...在哪个线程执行,跟队列类型有关
});
五.一次性代码static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只执行1次的代码(这里面默认是线程安全的)
});
dispatch_group_tgroup = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//执行1个耗时的异步操作
});
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//执行1个耗时的异步操作
});
dispatch_group_notify(group,dispatch_get_main_queue(),^{
//等前面的异步操作都执行完毕后,回到主线程...
});
七.单例模式
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
static id_instance;
+ (id)allocWithZone:(struct _NSZone*)zone {
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedMusicTool {
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
- (id)copyWithZone:(struct _NSZone*)zone {
return _instance;
}
- (id)retain { return self; }
- (NSUInteger)retainCount { return 1; }
- (oneway void)release{}
- (id)autorelease { return self; }
//1.串行队列 + 同步执行函数
//结论1. 没有开启新线程
//结论2. 按顺序执行
//开发中布经常用
- (void)serialAddSync
{
NSLog(@"serialAddSycn%@",[NSThread currentThread]);
//1.创建串行队列
dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
//2.同步执行函数 将任务添加到串行队列中 交给同步执行函数执行
dispatch_sync(queue, ^{
//任务1
NSLog(@"耗时任务1:%@",[NSThread currentThread]);
});
//往串行队列中 添加多个任务
dispatch_sync(queue, ^{
//任务2
NSLog(@"耗时任务2:%@",[NSThread currentThread]);
});dispatch_sync(queue, ^{
//任务3
NSLog(@"耗时任务3:%@",[NSThread currentThread]);
});
}
//2.串行队列 + 异步执行函数
//结论1.开启新线程
//2.同一队列的任务由同一个线程来执行,肯定是按顺序执行
//一个串行队列对应一条线程
- (void)serialAddAsync{
NSLog(@"serialAddAsycnBegan%@",[NSThread currentThread]);
//1.创建串行队列
dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue1 = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
//2.异步执行函数 将任务添加到串行队列中 交给异步执行函数执行
dispatch_async(queue, ^{
//任务1
NSLog(@"耗时任务1:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任务2
NSLog(@"耗时任务2:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任务3
NSLog(@"耗时任务3:%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
//任务1
NSLog(@"耗时任务4:%@",[NSThread currentThread]);
});
dispatch_async(queue1, ^{
//任务2
NSLog(@"耗时任务5:%@",[NSThread currentThread]);
});
NSLog(@"serialAddAsycnEnd%@",[NSThread currentThread]);
}
//3.并发队列 + 同步执行函数
//没有开启新线程
//任务顺序执行(在同一线程(当前线程)中执行)
- (void)concurrentAddSync
{
NSLog(@"concurrentAddSync%@",[NSThread currentThread]);
//1.创建全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.将任务添加到队列中
dispatch_sync(queue, ^{
//任务
NSLog(@"任务1:%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
//任务
NSLog(@"任务2:%@",[NSThread currentThread]);
});dispatch_sync(queue, ^{
//任务
NSLog(@"任务3:%@",[NSThread currentThread]);
});
}
//4.并发队列 + 异步执行函数(开发中使用最多)
//开启多条线程
//任务同时执行的
- (void)concurrentAddAsync {
NSLog(@"concurrentAddAsync%@",[NSThread currentThread]);
//1.创建全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.将任务添加到队列
dispatch_async(queue, ^{
//任务
NSLog(@"任务1:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任务
NSLog(@"任务2:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任务
NSLog(@"任务3:%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
//任务
NSLog(@"任务4:%@",[NSThread currentThread]);
});
}
//5.主队列 + 同步执行函数
//1.如果当前线程为主线程 会照成主队列和主线程中的任务相互等待 无法执行 主线程卡死
//主队列的任务都交给主线程执行
//主队列和主线程中的任务相互等待 无法执行
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"renwu1%@",[NSThread currentThread]);
});
NSLog(@"touchesEnd%@",[NSThread currentThread]);
//6.主队列 + 异步执行函数 (开发中需要回到主线程 使用 主队列 + 异步函数)
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"renwu1%@",[NSThread currentThread]);
});
NSLog(@"touchesEnd%@",[NSThread currentThread]);