1 串行队列,异步执行------所有异步添加到串行队列上的任务,都会在串行队列所创建的那个线程中去执行。
dispatch_queue_t serialQueue = dispatch_queue_create("com.zijie.serial", DISPATCH_QUEUE_SERIAL); NSThread *thread = [NSThread currentThread]; NSLog(@"1-----%@", thread); dispatch_async(serialQueue, ^{ NSLog(@"2----%@", [NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"3----%@", [NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"4----%@", [NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"5----%@", [NSThread currentThread]); }); // 打印结果: 2017-02-24 11:09:40.454 YYGCDDemo[73122:8089151] 1-----<NSThread: 0x7a0536b0>{number = 1, name = main} 2017-02-24 11:09:40.455 YYGCDDemo[73122:8089185] 2----<NSThread: 0x79772a10>{number = 2, name = (null)} 2017-02-24 11:09:40.456 YYGCDDemo[73122:8089185] 3----<NSThread: 0x79772a10>{number = 2, name = (null)} 2017-02-24 11:09:40.456 YYGCDDemo[73122:8089185] 4----<NSThread: 0x79772a10>{number = 2, name = (null)} 2017-02-24 11:09:40.456 YYGCDDemo[73122:8089185] 5----<NSThread: 0x79772a10>{number = 2, name = (null)}
2 串行队列,同步执行---------都会在主线程执行
dispatch_queue_t serialQueue = dispatch_queue_create("com.zijie.serial", DISPATCH_QUEUE_SERIAL); NSThread *thread = [NSThread currentThread]; NSLog(@"1-----%@", thread); dispatch_sync(serialQueue, ^{ NSLog(@"2----%@", [NSThread currentThread]); }); dispatch_sync(serialQueue, ^{ NSLog(@"3----%@", [NSThread currentThread]); }); dispatch_sync(serialQueue, ^{ NSLog(@"4----%@", [NSThread currentThread]); }); dispatch_sync(serialQueue, ^{ NSLog(@"5----%@", [NSThread currentThread]); }); // 打印结果: 2017-02-24 11:17:17.158 YYGCDDemo[73250:8105871] 1-----<NSThread: 0x79721be0>{number = 1, name = main} 2017-02-24 11:17:17.159 YYGCDDemo[73250:8105871] 2----<NSThread: 0x79721be0>{number = 1, name = main} 2017-02-24 11:17:17.159 YYGCDDemo[73250:8105871] 3----<NSThread: 0x79721be0>{number = 1, name = main} 2017-02-24 11:17:17.160 YYGCDDemo[73250:8105871] 4----<NSThread: 0x79721be0>{number = 1, name = main} 2017-02-24 11:17:17.160 YYGCDDemo[73250:8105871] 5----<NSThread: 0x79721be0>{number = 1, name = main}
3 并行队列,异步执行-------会启动多个线程并行执行,完成结果没有顺序
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zijie.concurrent", DISPATCH_QUEUE_CONCURRENT); NSThread *thread = [NSThread currentThread]; NSLog(@"1-----%@", thread); dispatch_async(concurrentQueue, ^{ [@"string1" length]; NSLog(@"2----%@", [NSThread currentThread]); }); dispatch_async(concurrentQueue, ^{ [@"string2" length]; NSLog(@"3----%@", [NSThread currentThread]); }); dispatch_async(concurrentQueue, ^{ [@"string3" length]; NSLog(@"4----%@", [NSThread currentThread]); }); dispatch_async(concurrentQueue, ^{ [@"string4" length]; NSLog(@"5----%@", [NSThread currentThread]); }); // 打印结果: 2017-02-24 11:22:29.054 YYGCDDemo[73385:8117872] 1-----<NSThread: 0x7c12eb20>{number = 1, name = main} 2017-02-24 11:22:29.056 YYGCDDemo[73385:8118127] 5----<NSThread: 0x7be2bba0>{number = 5, name = (null)} 2017-02-24 11:22:29.056 YYGCDDemo[73385:8118016] 3----<NSThread: 0x7be2f4c0>{number = 3, name = (null)} 2017-02-24 11:22:29.056 YYGCDDemo[73385:8118010] 2----<NSThread: 0x7c0611e0>{number = 2, name = (null)} 2017-02-24 11:22:29.057 YYGCDDemo[73385:8118126] 4----<NSThread: 0x7c0616b0>{number = 4, name = (null)}
4 并发队列,同步执行-------都会在主线程执行-------没有开启新线程,任务逐个执行
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zijie.concurrent", DISPATCH_QUEUE_CONCURRENT); NSThread *thread = [NSThread currentThread]; NSLog(@"1-----%@", thread); dispatch_sync(concurrentQueue, ^{ NSLog(@"2----%@", [NSThread currentThread]); }); dispatch_sync(concurrentQueue, ^{ NSLog(@"3----%@", [NSThread currentThread]); }); dispatch_sync(concurrentQueue, ^{ NSLog(@"4----%@", [NSThread currentThread]); }); dispatch_sync(concurrentQueue, ^{ NSLog(@"5----%@", [NSThread currentThread]); }); // 打印结果: 2017-02-24 11:19:05.166 YYGCDDemo[73292:8110126] 1-----<NSThread: 0x7bf48c80>{number = 1, name = main} 2017-02-24 11:19:05.169 YYGCDDemo[73292:8110126] 2----<NSThread: 0x7bf48c80>{number = 1, name = main} 2017-02-24 11:19:05.169 YYGCDDemo[73292:8110126] 3----<NSThread: 0x7bf48c80>{number = 1, name = main} 2017-02-24 11:19:05.169 YYGCDDemo[73292:8110126] 4----<NSThread: 0x7bf48c80>{number = 1, name = main} 2017-02-24 11:19:05.170 YYGCDDemo[73292:8110126] 5----<NSThread: 0x7bf48c80>{number = 1, name = main}
5 计时器 dispatch_source_t
NSTimer受runloop的影响,由于runloop需要处理很多任务,导致NSTimer的精度降低,在日常开发中,如果我们需要对定时器的精度要求很高的话,可以考虑dispatch_source_t去实现 。dispatch_source_t精度很高,系统自动触发,系统级别的源。
注意:dispatch_source_t必须定义为全局变量,因为如果是局部变量,dispatch_source_create创建后,没有对象持有它,当代码执行结束后,就会释放。
暂停计时器:dispatch_suspend(_timer);
恢复计时器:dispatch_resume(_timer);
// 实现倒计时功能
void btnClick:(id)sender {
__block NSInteger seconds = 59; dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue); dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ NSLog(@"---------%d", seconds); seconds -= 1; if (seconds < 0) { dispatch_source_cancel(_timer); } });
// 启动定时器 dispatch_resume(_timer);
}