ios多线程操作(六)—— GCD全局队列与主队列
dispatch_queue_t q = dispatch_get_global_queue(long identifier, unsigned long flags)
QOS_CLASS_DEFAULT 0x15, 默认(不是给程序员使用的,用来重置对列使用的)
QOS_CLASS_UTILITY 0x11, 实用工具(耗时操作,可以使用这个选项)
QOS_CLASS_BACKGROUND 0x09, 后台
QOS_CLASS_UNSPECIFIED 0x00, 未指定
iOS 7.0 之前 优先级
DISPATCH_QUEUE_PRIORITY_HIGH 2 高优先级
DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认优先级
DISPATCH_QUEUE_PRIORITY_LOW (-2) 低优先级
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
dispatch_queue_t q = dispatch_get_global_queue(0, 0); // 2. 异步执行 for (int i = 0; i < 10; ++i) { dispatch_async(q, ^{ NSLog(@"%@ %d", [NSThread currentThread], i); }); } NSLog(@"come here");
// 1. 队列 dispatch_queue_t q = dispatch_get_global_queue(0, 0); // 2. 任务 void (^task)() = ^ { dispatch_sync(q, ^{ NSLog(@"Login %@", [NSThread currentThread]); }); dispatch_async(q, ^{ NSLog(@"Download A %@", [NSThread currentThread]); }); dispatch_async(q, ^{ NSLog(@"Download B %@", [NSThread currentThread]); }); }; // 3. 异步执行 task dispatch_async(q, task);
dispatch_queue_t q = dispatch_get_main_queue();
dispatch_queue_t q = dispatch_get_main_queue(); // 2. 异步执行 for (int i = 0; i < 10; ++i) { dispatch_async(q, ^{ NSLog(@"%@ - %d", [NSThread currentThread], i); }); } NSLog(@"线程休眠1s"); [NSThread sleepForTimeInterval:1.0]; NSLog(@"come here - %@",[NSThread currentThread]);
// 1.队列 dispatch_queue_tq = dispatch_get_main_queue(); NSLog(@"now I'm here"); // 2. 同步执行 dispatch_sync(q, ^{ NSLog(@"%@", [NSThreadcurrentThread]); }); NSLog(@"come here");
// 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, 0); // 任务 void (^task)() = ^ { NSLog(@"%@", [NSThread currentThread]); // 主队列上执行同步任务 dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"come here %@", [NSThread currentThread]); }); NSLog(@"hahaha %@", [NSThread currentThread]); }; // 异步执行任务 dispatch_async(q, task); NSLog(@"now i'm here - %@",[NSThread currentThread]);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。