OC线程-GCD(Grand Central Dispatch)

    // 1.获得全局的并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    // 2.获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();

1.下载单张图片

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"--download--%@", [NSThread currentThread]);
        // 下载图片
        NSURL *url = [NSURL URLWithString:@"http://news.baidu.com/z/resource/r/image/fanlu.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
        UIImage *image = [UIImage imageWithData:data];
        
        // 回到主线程显示图片
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"curThread = %@", [NSThread currentThread]);
            self.imageView.image = image;
        });
    });

2.Group ,group_notify

   // 创建一个组
    dispatch_group_t group = dispatch_group_create();
    
    // 开启一个任务下载图片1
    __block UIImage *image1 = nil;
    dispatch_group_async(group, global_queue, ^{
        image1 = [self imageWithURL:@"http://news.baidu.com/test01.jpg"];
    });
    
    // 开启一个任务下载图片2
    __block UIImage *image2 = nil;
    dispatch_group_async(group, global_queue, ^{
        image2 = [self imageWithURL:@"http://news.baidu.com/z/resource/test02.jpg"];
    });
    
    // 同时执行下载图片1\下载图片2操作
    
    // 等group中的所有任务都执行完毕, 再回到主线程执行其他操作
    dispatch_group_notify(group, main_queue, ^{
        self.imageView1.image = image1;
        self.imageView2.image = image2;
        
        // 合并
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
        [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
        [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
        self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        // 关闭上下文
        UIGraphicsEndImageContext();
    });

3.处理读者与写者问题dispatch_barrier_async

1.因为只有并发队列才会有不安全的问题, 所以需要自定义并发队列来管理:

@property (nonatomic, strong) dispatch_queue_t concurrentPhotoQueue;

2.写者:

- (void)addPhoto:(Photo *)photo { 
    if (photo) { // 1 
        dispatch_barrier_async(self.concurrentPhotoQueue, ^{ // 2 
            [_photosArray addObject:photo]; // 3 
            dispatch_async(dispatch_get_main_queue(), ^{ // 4 
                [self postContentAddedNotification]; 
            }); 
        }); 
    } 
}

3.读者:

- (NSArray *)photos {
     __block NSArray *array; // 1 
     dispatch_sync(
        self.concurrentPhotoQueue, ^{ // 2 
            array = [NSArray arrayWithArray:_photosArray]; // 3 
      }); 
      return array; 
}

4.初始化自定义的并发队列:

+ (instancetype)sharedManager {
    static PhotoManager *sharedPhotoManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedPhotoManager = [[PhotoManager alloc] init];
        sharedPhotoManager->_photosArray = [NSMutableArray array]; // ADD THIS:
        sharedPhotoManager->_concurrentPhotoQueue = dispatch_queue_create("com.selander.GooglyPuff.photoQueue", DISPATCH_QUEUE_CONCURRENT); });
    return sharedPhotoManager;
    
}

参考

  1. http://www.raywenderlich.com/60749/grand-central-dispatch-in-depth-part-1
  2. http://www.cnblogs.com/ludashi/p/5336169.html

你可能感兴趣的:(OC线程-GCD(Grand Central Dispatch))