当仓央嘉措遇上纳兰性德iOS DeveloperiOS开发技术分享

GCD技术

2017-02-21  本文已影响200人  印林泉

https://github.com/yinlinqvan/LearnGCD

  • 基本概念
  • 任务与队列
  • 队列的创建
  • 线程间的通信
  • 其他用法

基本概念


任务和队列

根据影响点区分


队列的创建


工程模板:CGD queue


- (void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //[self concurrentSync];
    //[self concurrentAsync];
    //[self globalSync];
    //[self globalAsync];
    //[self mainSync];//死锁
    //[self serialSync];
}

#pragma mark - 并发队列 + 同步任务:没有启动新的线程,任务是逐个执行的
- (void)concurrentSync {
    //创建并发队列
    dispatch_queue_t queue= dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);
    //在队列里面添加任务
    //同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

#pragma mark - 并发队列 + 异步任务
- (void)concurrentAsync {
    //创建并发队列
    dispatch_queue_t queue= dispatch_queue_create("Queue",DISPATCH_QUEUE_CONCURRENT);
    //异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue,^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

#pragma mark - 全局队列 + 同步任务:没有启动新的线程,任务是逐个执行的
- (void)globalSync {
    //创建全局队列
    dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

#pragma mark - 全局队列 + 异步任务:开启了新的线程,任务是并发执行的
- (void)globalAsync {
    //创建全局队列
    dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue,^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

#warning - 死锁
#pragma mark - 主队列 + 同步任务:卡死了!!!死锁
- (void)mainSync {
    //创建主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

#pragma mark - 主队列 + 异步队列:没有开启新的线程,任务是逐个完成的
- (void)serialSync {
    //创建主队列
    dispatch_queue_t queue = dispatch_queue_create("queue", NULL);
    //异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===2===%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue,^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===3===%@", [NSThread currentThread]);
        }
    });
}

线程间的通信

#pragma mark - 线程间的通信
- (void)downloadImage {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //耗时操作
        NSURL *url = [NSURL URLWithString:@""];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        //回归到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            //显示图片;
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.image = image;
        });
    });
}

其他用法

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //2秒后异步执行这里的代码...
    });
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //只执行1次的代码(这里面默认是线程安全的)
    });
    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(), ^{
        //等前面的异步操作都执行完毕后,回到主线程...
    });

- (void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //[self delay];
    //[self once];
    //[self group];
}

#pragma mark - 延时操作
- (void)delay {
    //NSObject
    //[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

    //NSTimer
    //[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];

    //GCD
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self run];
    });
}

- (void)run {
    NSLog(@"run");
}

#pragma mark - 一次操作
- (void)once {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"一次性");
    });
}

#pragma mark - 多次操作
- (void)apply {
    dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index){
        NSLog(@"===%ld===", index);
    });
}

#pragma mark - 队列组
- (void)group {
    //创建组
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===");
        }
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i = 0; i< 5; i++) {
            NSLog(@"===1===");
        }
    });
    //当1、2完成后,再去执行3
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"===3===");
    });
}
上一篇下一篇

猜你喜欢

热点阅读