#pragma mark - 1异步并发 - (void)asyncGlobal { // 取得全局并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 使用异步调用 dispatch_async(queue, ^{ NSLog(@"result1, %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"result2, %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"result3, %@",[NSThread currentThread]); }); } #pragma mark - 2异步串行 - (void)asyncSerial { // 创建串行队列 dispatch_queue_t queue = dispatch_queue_create("Serial", NULL); // 使用异步调用 dispatch_async(queue, ^{ NSLog(@"result1, %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"result2, %@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"result3, %@",[NSThread currentThread]); }); } #pragma mark - 3同步并发 - (void)syncGlobal { // 取得全局并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 使用同步调用 dispatch_sync(queue, ^{ NSLog(@"result1, %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"result2, %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"result3, %@",[NSThread currentThread]); }); } #pragma mark - 4同步串行 - (void)syncSerial { // 创建串行队列 dispatch_queue_t queue = dispatch_queue_create("Serial", NULL); // 使用同步调用 dispatch_sync(queue, ^{ NSLog(@"result1, %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"result2, %@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"result3, %@",[NSThread currentThread]); }); }
同步 (sync) 不开起新的线程
并发队列: 不开起新的线程(用默认的主线程)
串行队列: 不开起新的线程(用默认的主线程)
异步 (async) 开启新的线程
并发队列: 能开启多条线程(取出队列中的任务,子线程执行;接着取出队列中后面的任务,开辟其他线程执行这条任务....)
串行队列: 只开启一条子线程(取出队列中的任务,子线程执行,执行完毕后,继续取出任务,子线程继续执行....)
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//执行耗时的异步操作...
dispatch_async(dispatch_get_main_queue(),^{
// 回到主线程,执行UI刷新操作
});
});
上面的代码片段是经典的线程间通信使用方法。
Example:
从网络下载图片资源,并显示到主线程UI上面
// 并发队列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 开启子线程 dispatch_async(queue, ^{ NSURL *url = [NSURL URLWithString:@"http://pic.cnr.cn/list/201209/W020120914588236390160.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; // 讲image信息在主线程上面呈现(这样就不会卡死了) dispatch_async(dispatch_get_main_queue(), ^{ self.myImageView.image = image; }); });
dispatch_group_t group = 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(), ^{ // 等前面的异步操作都执行完毕后,回到主线程... });
#pragma mark - 并发下载多张图片 - (void)downloadImage { dispatch_group_t group = dispatch_group_create(); __block UIImage *image1 = nil; dispatch_group_async(group, GlobalQueue, ^{ image1 = [self imageWithUrl:@"http://www.2qqtouxiang.cn/uploads/allimg/100917/1_100917230241_2.jpg"]; }); __block UIImage *image2 = nil; dispatch_group_async(group, GlobalQueue, ^{ image2 = [self imageWithUrl:@"http://img3.douban.com/view/photo/albumicon/public/p1446733286.jpg"]; }); // 上面两个GCD是异步同时下载图片的,下面这句代码是等到两张图片全部下载完毕后便会执行 dispatch_group_notify(group, MainQueue, ^{ self.image1.image = image1; self.image2.image = image2; // 拼接图片 UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0.0); [image1 drawInRect:CGRectMake(0, 0, 50, 100)]; [image2 drawInRect:CGRectMake(50, 0, 50, 100)]; self.myImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }); } - (UIImage *)imageWithUrl:(NSString *)urlStr { NSURL *url = [NSURL URLWithString:urlStr]; NSData *data = [NSData dataWithContentsOfURL:url]; return [UIImage imageWithData:data]; }
#pragma mark - 延时执行 - (void)delay { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"延时3秒执行~~~"); }); } #pragma mark - 不管调用多少次只执行一次 - (void)doOnce { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"一次性执行"); }); }
原文地址:http://blog.csdn.net/sinat_27706697/article/details/45640299