[多线程之三]-gcd操作练习

/*//先看效果
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL* url = [[NSURL alloc] initWithString:@"http://avatar.csdn.net/3/0/F/1_liyanq528.jpg"];
        NSData* data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage* img = [[UIImage alloc] initWithData:data];
        if (img) {
            dispatch_async(dispatch_get_main_queue(), ^{
                 self.MyImageView.image = img;
            });
            
        }
    });*/
    
    /*//验证Serial queue的FIFO特性
    dispatch_queue_t queue = dispatch_queue_create([mystr UTF8String], DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];//延迟了2秒还是第一个执行
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
    });
    dispatch_async(queue, ^{
        NSLog(@"3");
    });
    
    //验证CONCURRENT queue的FIFO特性
    //dispatch_queue_t queueCur = dispatch_queue_create([mystr UTF8String], DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queueCur = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //上面用哪个的结果都是一样的,看来dispatch_get_global_queue得到的就是并行队列
    dispatch_async(queueCur, ^{
        [NSThread sleepForTimeInterval:2];//延迟了2秒最后一个执行
        NSLog(@"a");
    });
    dispatch_async(queueCur, ^{
        NSLog(@"b");
    });
    dispatch_async(queueCur, ^{
        NSLog(@"c");
    });*/
    
    /*//dispatch_group_async使用->SERIAL QUEUE;   这个看着很清晰,可以把前后有联系或依赖的放进去。
    NSString* str = @"hello";
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t gqueue = dispatch_queue_create([str UTF8String], DISPATCH_QUEUE_SERIAL);
    NSString* qName = [NSString stringWithUTF8String:dispatch_queue_get_label(gqueue)];
    dispatch_async(gqueue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"延迟了2秒,我先入队列的!");
    });
    dispatch_async(gqueue, ^{
        NSURL* url = [[NSURL alloc] initWithString:@"http://avatar.csdn.net/3/0/F/1_liyanq528.jpg"];
        NSData* data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage* img = [[UIImage alloc] initWithData:data];
        if (img) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.MyImageView.image = img;
            });
            
        }
    });
    dispatch_group_async(group, gqueue, ^{
        NSLog(@"A");
            });
    dispatch_group_async(group, gqueue, ^{
        NSLog(@"B");
    });
    dispatch_group_async(group, gqueue, ^{
        NSLog(@"C");
    });
    
    dispatch_group_notify(group, gqueue, ^{
        NSLog(@"%@这个队列的内容都执行完了",qName);
    });*/
    
    /*//dispatch_group_async使用->CONCURRENT QUEUE; 这个是先到先得,并发处理
    NSString* cStr = @"world";
    dispatch_group_t cgroup = dispatch_group_create();
    dispatch_queue_t cqueue = dispatch_queue_create([cStr UTF8String], DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(cqueue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"延迟了2秒,我先入队列的!");
    });
    dispatch_async(cqueue, ^{
        NSURL* url = [[NSURL alloc] initWithString:@"http://avatar.csdn.net/3/0/F/1_liyanq528.jpg"];
        NSData* data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage* img = [[UIImage alloc] initWithData:data];
        if (img) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.MyImageView.image = img;
            });
            
        }
    });
    dispatch_group_async(cgroup, cqueue, ^{
        NSLog(@"A");
    });
    dispatch_group_async(cgroup, cqueue, ^{
        NSLog(@"B");
    });
    dispatch_group_async(cgroup, cqueue, ^{
        NSLog(@"C");
    });
    
    dispatch_group_notify(cgroup, cqueue, ^{
        NSLog(@"%@这个队列的内容都执行完了",cStr);
    });*/
    
    //dispatch_barrier_async的使用1
    /*//结果可能是abc,也可能是bac,但c肯定是最后的。
    NSString* str = @"Hello";
    dispatch_queue_t queue = dispatch_queue_create([str UTF8String], DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"a");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"b");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"c");
    });*/
    
    /*//dispatch_barrier_async的使用2
    //这个怎么就不好使了呢?想不明白,都是CONCURRENT对列么。自带的和创建的执行效果不一样?
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"a");
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"b");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"c");
    });*/
    
    
    /*//dispatch_apply执行多少次
    //这里如果是concurrent类型队列会是5个1,但如果是serial队列是12345.
    NSString* str = @"Hello";
    __block NSInteger index = 1;
    dispatch_queue_t queue = dispatch_queue_create([str UTF8String], DISPATCH_QUEUE_SERIAL);
    dispatch_apply(5, queue, ^(size_t t)  {
        NSLog(@"%ld", index);
        index++;
    });*/
    
    //dispatch_once执行一次,最常用的就是单例模式了吧。
    
    /*利用了3个中午休息时间整理了这么多,先总结下,以后有新的发现再往上添加,关键是没有用到实际工程里面呢。
     1,gcd的操作非常简单容易用,而且马上就能看到效果。
     2,不需要知道什么临界区,信号量之类的线程同步的东西。
     3,不需要知道队列原理,线程池的工作方式。
     4,更新页面也提供了主线程的获取。
     5,执行一组任务后,还能获取通知。
     
     总之,苹果的这个发明真的很强大,需要自己慢慢领悟。
     参考http://blog.csdn.net/wildfireli/article/details/18668897*/


你可能感兴趣的:(ios,gcd)