前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GCD的常用方法总结

GCD的常用方法总结

原创
作者头像
大师级码师
修改2021-11-01 09:27:04
4050
修改2021-11-01 09:27:04
举报
文章被收录于专栏:大师级码师大师级码师
代码语言:javascript
复制
//    GCD常用方法

//————————————————————————————————————————————————————————————————————————————————

//串行队列

dispatch_queue_t queueSerial = dispatch_queue_create("jr", DISPATCH_QUEUE_SERIAL);


//并行队列

dispatch_queue_t queueConcu = dispatch_queue_create("jr2", DISPATCH_QUEUE_CONCURRENT);

//1、循环多次使用

//(1)添加串行队列,同步执行。可以当for循环使用(在主线程中)


//    dispatch_apply(3, queueSerial, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%@", [NSThread currentThread]);
//    });

//(2)添加并行队列,同步执行。可以当for循环使用(如果循环次数大于1,则开子线程)


//    dispatch_apply(3, queueConcu, ^(size_t t) {// 参数^(size_t),需要自己加一个^(size_t t),否则有问题
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%@", [NSThread currentThread]);
//    });

//(3)参数size_t的作用:打印的是0.1.2.3.。。的序号


//    dispatch_apply(3, queueSerial, ^(size_t t) {
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%li == %@", t, [NSThread currentThread]);
//    });

//(4)以上都是在主线程中进行的。如果想在子线程中执行,需要这样做


//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        dispatch_apply(3, queueConcu, ^(size_t t) {
//            [NSThread sleepForTimeInterval:1];
//            NSLog(@"%li == %@", t, [NSThread currentThread]);
//        });
//    });

//2、分组(可以监听组内子线程是否全部都执行完成)


//    dispatch_group_t group = dispatch_group_create();
//
//    dispatch_group_async(group, queueConcu, ^{
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%@", [NSThread currentThread]);
//    });
//    dispatch_group_async(group, queueConcu, ^{
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%@", [NSThread currentThread]);
//    });
//    dispatch_group_async(group, queueConcu, ^{
//        [NSThread sleepForTimeInterval:1];
//        NSLog(@"%@", [NSThread currentThread]);
//    });
//
//    dispatch_group_notify(group, queueConcu, ^{
//        NSLog(@"----%@", [NSThread currentThread]);
//    });

//3、延迟(写在这个块中的代码都是在主线程中执行的)



dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [NSThread sleepForTimeInterval:2];

    NSLog(@"----%@", [NSThread currentThread]);

});

 NSLog(@"33333%@", [NSThread currentThread]);

timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(touchesBegan) userInfo:nil repeats:YES];




//4.设置障碍(在同一队列中,只要添加了障碍,不管有没有创建多线程,他后边的任务都要跨国这个障碍,即等待这个障碍结束)


//    dispatch_async(queueConcu, ^{
//        NSLog(@"11111");
//    });
//
//    dispatch_barrier_async(queueConcu, ^{
//        [NSThread sleepForTimeInterval:5];
//    });
//
//    dispatch_async(queueConcu, ^{
//        NSLog(@"11111");
//    });
//    dispatch_async(queueConcu, ^{
//        NSLog(@"11111");
//    });

//5、让代码只执行一次


//    static dispatch_once_t oneToken;
//    dispatch_once(&oneToken, ^{
//        NSLog(@"=======");
//    });

//这个方法。常用在单例中</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档