OC 线程操作 - GCD队列组
1.队列组两种使用方法
2.队列组等待 wait
/** 新方法
队列组一般用在在异步操作,在主线程写队列组毫无任何作用
*/
- (void)GCD_Group_new_group___notify{
dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
dispatch_group_t group = dispatch_group_create();
/*
1.封装任务
2.把任务加到队列
3.会监听任务的执行情况
*/
dispatch_group_async(group, globalqueue, ^{
NSLog(@"1111---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"22222---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"333---%@---", [NSThread currentThread]);
}); // 一定要加上这行,不然不起任何作用
dispatch_group_notify(group, globalqueue, ^{
NSLog(@"完成----4444---%@---", [NSThread currentThread]);
}); /*
打印结果:
2018-06-28 10:18:44.191407+0800 5线程操作-GCD-快速迭代[7808:56262] 333---<NSThread: 0x6000002682c0>{number = 4, name = (null)}---
2018-06-28 10:18:44.191409+0800 5线程操作-GCD-快速迭代[7808:56266] 1111---<NSThread: 0x608000075300>{number = 3, name = (null)}---
2018-06-28 10:18:44.191434+0800 5线程操作-GCD-快速迭代[7808:56264] 22222---<NSThread: 0x600000266580>{number = 5, name = (null)}---
2018-06-28 10:18:44.191758+0800 5线程操作-GCD-快速迭代[7808:56264] 完成----4444---<NSThread: 0x600000266580>{number = 5, name = (null)}---
*/
//DISPATCH_TIME_FOREVER :等待队列所有任务执行完后 执行下面代码后面的内容
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"执行完了");
/*
打印结果:
2018-06-28 10:58:34.631990+0800 5线程操作-GCD-快速迭代[8500:86081] 1111---<NSThread: 0x60400007e180>{number = 3, name = (null)}---
2018-06-28 10:58:34.632041+0800 5线程操作-GCD-快速迭代[8500:86084] 22222---<NSThread: 0x60400007e4c0>{number = 4, name = (null)}---
2018-06-28 10:58:34.632065+0800 5线程操作-GCD-快速迭代[8500:86080] 333---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
2018-06-28 10:58:34.633261+0800 5线程操作-GCD-快速迭代[8500:86052] 执行完了
2018-06-28 10:58:34.633284+0800 5线程操作-GCD-快速迭代[8500:86080] 完成----4444---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
*/
}
//老式写法
- (void)GCD_GroupDemo_old___enter_leave{
dispatch_queue_t queue = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t globalqueue = dispatch_get_global_queue(, );
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group); // 旧 写法
dispatch_group_async(group, globalqueue, ^{
NSLog(@"离开---%@---", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_async(group, globalqueue, ^{
NSLog(@"222---%@---", [NSThread currentThread]);
});
dispatch_group_async(group, globalqueue, ^{
NSLog(@"333---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"111---%@---", [NSThread currentThread]);
});
/*
2018-06-28 10:25:06.997243+0800 5线程操作-GCD-快速迭代[7942:62493] 离开---<NSThread: 0x60c00007f8c0>{number = 3, name = (null)}---
2018-06-28 10:25:06.997286+0800 5线程操作-GCD-快速迭代[7942:62491] 333---<NSThread: 0x60800007d240>{number = 5, name = (null)}---
2018-06-28 10:25:06.997306+0800 5线程操作-GCD-快速迭代[7942:62489] 111---<NSThread: 0x608000260c80>{number = 6, name = (null)}---
2018-06-28 10:25:06.997287+0800 5线程操作-GCD-快速迭代[7942:62492] 222---<NSThread: 0x60c00007de40>{number = 4, name = (null)}---
*/
}
OC 线程操作 - GCD队列组的更多相关文章
- OC 线程操作 - GCD快速迭代
- (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...
- OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...
- OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // [self downImag ...
- OC 线程操作 - GCD使用 - 栅栏函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...
- OC线程操作-GCD介绍
1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.
- iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组
一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 GCD的优势 G ...
- 多线程 GCD队列组
// DYFViewController.m // 623-08-队列组 // // Created by dyf on 14-6-23. // Copyright (c) 2014年 ___ ...
- OC 线程操作2 - NSThread
方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...
- OC - GCD 队列组 - 下载图片画图
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self downloadIma ...
随机推荐
- vmware12共享windows的文件给虚拟的linux
1:首先我的vmware的版本是12的 点击vmware的虚拟机---------------------->设置------------------------>选项---------- ...
- oracle 所有 hint(转)
oracle 10g 有64个hints , 11g 增加到71 个, 下表中红色的代表已经过时的, 粗体的是11g 新增. Optimization Goals and Approaches (2) ...
- SpringBoot整合定时任务task
@SpringBootApplication //扫描 mybatis mapper 包路径 @MapperScan(basePackages = "com.imooc.mapper&quo ...
- Out 与 Ref 关键字的区别
相同点:既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功效, ...
- c++官方文档-class
#include <iostream> using namespace std; class Circle { double radius; public: Circle(double r ...
- bootstrap file input 多图片上传编辑THINKPHP5
{layout name="layout" title="文章添加" /} <form id="defaultForm" role=& ...
- three3D地图设置两点之间的连线
点:可以用THREE.Vector3D来表示 现在来看看怎么定义个点,假设有一个点x=4,y=8,z=9.你可以这样定义它: var point1 = new THREE.Vecotr3(4,8,9) ...
- CentOs - 使用ssh key远程登录
环境: 服务器端CentOs,本地OS X 服务器端: 1. 安装openssl使实现ssl协议 2. 将本地的pub key加入信任列表 本地: 1. 生成pub key 2. 配置ssh别名使登陆 ...
- maven 在pom.xml 中指定仓库位置
...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...
- eclipse模板
文件(Files)注释标签: /** * @Title: ${file_name} * @Package ${package_name} * @Description: ${todo}(用一句话描述该 ...