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 ...
随机推荐
- 华为P10的内存门和闪存门的检测方法
用android的终端模拟器,进入以后进入界面,输入命令ls /proc/fs/*,可以查看是否ufs还是emmc硬盘:用devcheck可以查看到手机的内存是否是DDR3还是DDR4:用androb ...
- git修改用户名和邮箱
用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. 1.查看用户名和地址 git config user.name git config us ...
- Web API 源码剖析之默认配置(HttpConfiguration)
Web API 源码剖析之默认配置(HttpConfiguration) 我们在上一节讲述了全局配置和初始化.本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpC ...
- 显示器如何显示一个YUV422格式的图形
记录在开发过程中对知识点的一些理解: 在开发渲染程序的过程中,需要对视屏文件进行解码解码后特效文件的叠加,使用的技术是(FFmpeg+DirectX) 解码出来的视屏数据格式是YUYV,使用Direc ...
- KVM虚拟机网络闪断分析
https://www.cnblogs.com/Bozh/p/5484838.html 背景 公司云平台的机器时常会发生网络闪断,通常在10s-100s之间. 异常情况 VM出现问题时,表现出来的情况 ...
- C# List<string>和ArrayList用指定的分隔符分隔成字符串
原文地址:https://www.cnblogs.com/ahwwmb/p/4166707.html 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符 List<string> ...
- uva-10004-俩色图验证
题意: 在1976年,四色猜想被一个计算机助手提出,这个理论表示对任意一个地图的上色都只需要四种颜色,地图内每一个区块和相邻的区块颜色都不相同.你现在被要求解决一个相似但相对简单的问题.给你任意一个连 ...
- OpenACC Julia 图形
▶ 书上的代码,逐步优化绘制 Julia 图形的代码 ● 无并行优化(手动优化了变量等) #include <stdio.h> #include <stdlib.h> #inc ...
- zabbix监控vCenter报错,无法自动发现主机
公司机房停电检修,检修完成后重新上电,发现VCSA起不来了,尝试多次无法解决,无奈只好重装.重装VCSA 6.5U2之后又发现无法自动发现主机,报错如下: 'config.vpxd.stats.max ...
- 15. Studio上字符串转整形、整形转字符串例子
var v1=ABS_SQLVALUE("select 1 from dual");var v2=ABS_SQLVALUE("select 2 from dual&quo ...