- (void)groupEvent{
//创建线程
dispatch_group_t group =dispatch_group_create();
dispatch_queue_t globalQueue=dispatch_get_global_queue(, ); dispatch_group_enter(group); //模拟多线程耗时操作
dispatch_group_async(group, globalQueue, ^{
sleep();
NSLog(@"%@---block1结束。。。",[NSThread currentThread]);
dispatch_group_leave(group);
}); NSLog(@"%@---1NSLOG结束。。。",[NSThread currentThread]); dispatch_group_enter(group);
//模拟多线程耗时操作
dispatch_group_async(group, globalQueue, ^{
sleep();
NSLog(@"%@---block2结束。。。",[NSThread currentThread]);
dispatch_group_leave(group);
}); NSLog(@"%@---2NSLOG结束。。。",[NSThread currentThread]);
//线程全部结束
dispatch_group_notify(group, dispatch_get_global_queue(, ), ^{
NSLog(@"%@---全部结束。。。",[NSThread currentThread]);
}); }

上面的代码的后台输出是:

-- ::27.853 CPMNetworking[:] <NSThread: 0x600000068600>{number = , name = main}---1NSLOG结束。。。
-- ::27.856 CPMNetworking[:] <NSThread: 0x600000068600>{number = , name = main}---2NSLOG结束。。。
-- ::30.923 CPMNetworking[:] <NSThread: 0x608000263f00>{number = , name = (null)}---block1结束。。。
-- ::30.930 CPMNetworking[:] <NSThread: 0x6000002647c0>{number = , name = (null)}---block2结束。。。
-- ::30.930 CPMNetworking[:] <NSThread: 0x6000002647c0>{number = , name = (null)}---全部结束。。。

可以发现,因为block内被加了休眠sleep,所以block外的NSLOG操作被先执行了。而在block1被执行完毕后,block2和线程结束的部分的内容才被执行

[OC] 线程 dispatch_group_t的更多相关文章

  1. OC 线程操作 - GCD队列组

    1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...

  2. OC 线程操作1 - pthread

    #import "ViewController.h" #import <pthread.h> //1.需要包含这个头文件 @interface ViewControll ...

  3. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...

  4. OC 线程操作3 - NSOperation

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  5. OC 线程操作 - GCD快速迭代

    - (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...

  6. OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...

  7. OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...

  8. OC 线程操作3 - NSOperation 实现线程间通信

    #import "ViewController.h" @interface ViewController () /** 图片 */ @property (weak, nonatom ...

  9. OC 线程操作 - GCD使用 - 栅栏函数

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...

随机推荐

  1. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  2. 五十五、linux 编程——TCP 连接和关闭过程及服务器的并发处理

    55.1 TCP 连接和关闭过程 55.1.1 介绍 建立连接的过程就是三次握手的过程:客户端发送 SYN 报文给服务器,服务器回复 SYN+ACK 报文,客户机再发送 ACK 报文. 关闭连接的过程 ...

  3. [C++]UVaLive7324 ASCII Addtion

    Description Nowadays, there are smartphone applications that instantly translate text and even solve ...

  4. js时间戳转日期

    //时间戳转日期 2017-04-30 13:20 //type=1--> 2017-04-30 13:20 //type=2-->2018年08月 //type=3-->2018- ...

  5. 浅入深出Vue:工具准备之PostMan安装配置及Mock服务配置

    浅入深出Vue之工具准备(二):PostMan安装配置 由于家中有事,文章没顾得上.在此说声抱歉,这是工具准备的最后一章. 接下来就是开始环境搭建了~尽情期待 工欲善其事必先利其器,让我们先做好准备工 ...

  6. error: No rule to make target '/usr/lib/libOpenNI.so', needed by 'bin/euroc_rectify'。 停止。

    这类问题的出现说明程序在编译时,CMakeLists.txt 文件没有找到OpenNI.so, 即 Pangolin库未安装或破损. 重新安装pangolin库即可.(亲测)

  7. 服务器 隐藏php版本,nginx版本号等

    隐藏php版本号: 打开php.ini配置文件  找到 expose_php 关键修改为 off 即可 重启后 web头部就不会有了 隐藏 nginx 服务器版本号: 打开nginx配置文件,在htt ...

  8. windows 安装docker报错:Error checking TLS connection: ssh command error: command : ip addr show

    今天安装docker部署的时候总是再报这个错误. 报错的原因是初始化的时候出错了. 在docker 安装目录下有一个文件,如下图所示 将它复制到你电脑用户名目录下生成.docker 的文件夹中,如下图 ...

  9. 初识C语言(二)

    C语言标识符的命名规则 变量或者函数起的名字就是标识符,而且C语言的标识符有它自己的命名规则: 标识符的长度最好不要超过8位,因为在一些版本的C语言中标示符的前八位是有效的,所以当两个标识符的前八位相 ...

  10. swoole 版本查看

    php --ri swoole php --ri swoole | grep Version  查看swoole版本 php -m | grep swoole  查看swoole拓展是否安装成功(ph ...