GXiOS

iOS开发--GCD

2019-06-08  本文已影响44人  Caesar_62dd

前言:GCD是用的最多的多线程方法,本来以为很难,但是看了两天的视频发现其实并没有想象的那么可怕.

一、队列和同步异步方法.

各种队列执行效果.png

二、 GCD常用方法:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self groupQueue];
}
//1.同步+串行队列--->无法开启新线程,串行执行
- (void)serielSync{
//    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", NULL);
    dispatch_sync(queue, ^{
        NSLog(@"______%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"--222----%@", [NSThread currentThread]);
    });
}
//2.异步+串行队列--->开启一条新线程,串行执行
- (void)serielAsync{
//    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", NULL);
    dispatch_async(queue, ^{
        NSLog(@"______%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"--222----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"---333---%@", [NSThread currentThread]);
    });
}
//3.同步+并发队列--->无法开启新线程,串行执行
- (void)concurrentSync{
    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"_________%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"--222----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"---333---%@", [NSThread currentThread]);
    });
}
//4.异步+并发队列--->开启多条线程(不确定线程个数),并发执行
- (void)concurrentAsync{
    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"_________%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"--222----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"---333---%@", [NSThread currentThread]);
    });
}
//5.同步+全局主队列(全局串行队列)--->死锁
- (void)mainQueueSync{
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"----%@----",[NSThread currentThread]);
    });
}
//6.异步+全局主队列--->无法开启新线程,串行执行
- (void)mainQueueAsync{
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"----%@----",[NSThread currentThread]);
    });
}
//7.同步+全局并发队列--->无法开启新线程,串行执行
- (void)globalQueueSync{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(queue, ^{
        NSLog(@"--1--%@----", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"--2--%@----", [NSThread currentThread]);
    });
}
//8.异步+全局并发队列--->开启多条线程,并发执行
- (void)globalQueueAsync{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"--1--%@----", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"--2--%@----", [NSThread currentThread]);
    });
}
//9.GCD常用函数-after,延迟执行
- (void)delayQueue{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC) ), dispatch_get_main_queue(), ^{
        NSLog(@"---%@---", [NSThread currentThread]);
    });
}
//10.GCD常用函数-once 一次性代码(单例使用,不能作为懒加载使用)
- (void)onceQueue{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"---%@---", [NSThread currentThread]);
    });
    
}
//11.GCD常用函数-栅栏函数-barrier--->控制异步函数的执行顺序
- (void)barrierQueue{
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //栅栏函数不能在全局并发队列中使用,要自己创建并发队列(全局并发队列和手动创建并发队列的区别之一)
    dispatch_queue_t queue = dispatch_queue_create("download", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"---Download1---%@---",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"---Download2---%@---",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"++++++++++++++++++++++++++");
    });
    dispatch_async(queue, ^{
        NSLog(@"---Download3---%@---",[NSThread currentThread]);
    });
}
//12.GCD常用函数-快速迭代-apply--->无法控制执行顺序
- (void)applyQueue{
    NSString *from = @"/Users/gaoxiong/Desktop/from";
    NSString *to =@"/Users/gaoxiong/Desktop/to";
    NSFileManager *fileMagager = [NSFileManager defaultManager];
    //获取目录下所有文件
    NSArray *subPath = [fileMagager subpathsAtPath:from];
    NSLog(@"%@", subPath);
    NSInteger count = subPath.count;
    dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t index) {
        NSString *fullPathFrom = [from stringByAppendingPathComponent:subPath[index]];
        NSString *fullPathTo = [to stringByAppendingPathComponent:subPath[index]];
        [fileMagager moveItemAtPath:fullPathFrom toPath:fullPathTo error:nil];
        NSLog(@"=-----%@-----%@", subPath[index], [NSThread currentThread]);
    });
}
//13.GCD常用函数-gruop-线程组---->控制异步函数的执行顺序
- (void)groupQueue{
    dispatch_group_t gruop = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("com.gx.www", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_async(gruop, queue, ^{
        NSLog(@"---------1----%@", [NSThread currentThread]);
    });
    dispatch_group_async(gruop, queue, ^{
        NSLog(@"---------2----%@", [NSThread currentThread]);
    });
    dispatch_group_async(gruop, queue, ^{
        NSLog(@"---------3----%@", [NSThread currentThread]);
    });
    dispatch_group_notify(gruop, queue, ^{
        NSLog(@"------the last performence !!--- %@", [NSThread currentThread]);
    });
}
@end

上一篇下一篇

猜你喜欢

热点阅读