标签:
GCD默认已经提供了全局的并发队列供整个应用使用,所以可以不用手动创建。
dispatch_queue_t q = dispatch_get_global_queue(long identifier, unsigned long flags)
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]);
标签:
原文地址:http://www.cnblogs.com/zh-li/p/5140619.html