1.栅栏函数

  作用:控制线程的执行顺序

  注:栅栏函数不能使用全局并发队列

 -(void)barrier
{
//1.创建队列(并发队列)
dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ for (NSInteger i = ; i<; i++) {
NSLog(@"%zd-download1--%@",i,[NSThread currentThread]);
}
}); dispatch_async(queue, ^{ for (NSInteger i = ; i<; i++) {
NSLog(@"%zd-download2--%@",i,[NSThread currentThread]);
}
}); //栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"我是一个栅栏函数");
}); dispatch_async(queue, ^{ for (NSInteger i = ; i<; i++) {
NSLog(@"%zd-download3--%@",i,[NSThread currentThread]);
}
}); dispatch_async(queue, ^{ for (NSInteger i = ; i<; i++) {
NSLog(@"%zd-download4--%@",i,[NSThread currentThread]);
}
});
}

栅栏函数

2.延迟执行

 -(void)delay
{
NSLog(@"----");
//表名2秒钟之后调用run
// [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES]; /*
第一个参数:延迟时间
第二个参数:要执行的代码
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(, ), ^{
NSLog(@"---%@",[NSThread currentThread]);
}); }

延迟执行

3.一次性代码

程序运行过程中只执行一次,执行一次后永远都不执行

 -(void)once
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"+++++++++");
});
}

一次性代码

4.快速迭代

 -(void)applay
{
// for (NSInteger i=0; i<10; i++) {
// NSLog(@"%zd--%@",i,[NSThread currentThread]);
// } //创建队列(并发队列)
dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT);
/*
第一个参数:迭代的次数
第二个参数:在哪个队列中执行
第三个参数:block要执行的任务
*/
dispatch_apply(, queue, ^(size_t index) {
NSLog(@"%zd--%@",index,[NSThread currentThread]);
});
}

快速迭代


     //文件在哪个地方(文件夹)

     NSString *form = @"/Users/xiaomage/Desktop/form";

     //要剪切到什么地方

     NSString *to = @"/Users/xiaomage/Desktop/to";

 NSFileManager *manager = [NSFileManager defaultManager];

 //获取文件夹下子文件/子文件夹的路径

 //可以获取到子文件和子文件夹的路径

     NSArray *subpaths =  [manager subpathsAtPath:form];

 //   可以获取到子文件和子文件夹的路径
NSDirectoryEnumerator *enumer = [manager enumeratorAtPath:to]; //只能获取子文件的路径 NSDirectoryEnumerator *enumer = [manager directoryContentsAtPath:form]; //创建队列(并发队列) dispatch_queue_t queue = dispatch_queue_create("com.downloadqueue", DISPATCH_QUEUE_CONCURRENT); NSInteger count = [subpaths count]; dispatch_apply(count, queue, ^(size_t index) { NSString *subpath = subpaths[index]; NSString *fullPath = [form stringByAppendingPathComponent:subpath]; //拼接目标文件全路径 NSString *fileName = [to stringByAppendingPathComponent:subpath]; //剪切操作 [manager moveItemAtPath:fullPath toPath:fileName error:nil]; NSLog(@"%@",[NSThread currentThread]); });

用快速迭代实现文件夹内容的转移

5.队列组

有这么1种需求

首先:分别异步执行2个耗时的操作

其次:等2个异步操作都执行完毕后,再回到主线程执行操作
 -(void)group
{
//下载图片1 //创建队列组
dispatch_group_t group = dispatch_group_create(); //1.开子线程下载图片
//创建队列(并发)
dispatch_queue_t queue = dispatch_get_global_queue(, ); dispatch_group_async(group, queue, ^{
//1.获取url地址
NSURL *url = [NSURL URLWithString:@"http://www.huabian.com/uploadfile/2015/0914/20150914014032274.jpg"]; //2.下载图片
NSData *data = [NSData dataWithContentsOfURL:url]; //3.把二进制数据转换成图片
self.image1 = [UIImage imageWithData:data]; NSLog(@"1---%@",self.image1);
}); //下载图片2
dispatch_group_async(group, queue, ^{
//1.获取url地址
NSURL *url = [NSURL URLWithString:@"http://img1.3lian.com/img2011/w12/1202/19/d/88.jpg"]; //2.下载图片
NSData *data = [NSData dataWithContentsOfURL:url]; //3.把二进制数据转换成图片
self.image2 = [UIImage imageWithData:data];
NSLog(@"2---%@",self.image2); }); //合成
dispatch_group_notify(group, queue, ^{ //开启图形上下文
UIGraphicsBeginImageContext(CGSizeMake(, )); //画1
[self.image1 drawInRect:CGRectMake(, , , )]; //画2
[self.image2 drawInRect:CGRectMake(, , , )]; //根据图形上下文拿到图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文
UIGraphicsEndImageContext(); dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"%@--刷新UI",[NSThread currentThread]);
});
});
}

用队列组合成网络上的两个图片

GCD的其他方法的更多相关文章

  1. GCD部分使用方法

    1,用gcd延迟运行任务 假设我们须要某个方法在一段时间后运行.那么我们经常会调用这个方案 - (void)viewDidLoad{ [super viewDidLoad]; [self perfor ...

  2. 网络与多线程---OC中多线程方法GCD(二)

    小编在前一篇中介绍了多线程实现的五种常用方法.在接下来所介绍的这种方法是最具有魅力的,最具有诱惑的实现多线程的方案---GCD 一.什么是GCD GCD是Grand Central Dispatch的 ...

  3. GCD in Swfit 3.0

    这里包括了Queue, Group, Barrier, Semaphore等内容.基本上常用的GCD对象和方法在Swift3.0的改变都囊括其中. 代码在这里:https://github.com/f ...

  4. GCD与block

    GCD技术多线程编程的三个技术  NSThread NSOperation GCD1.GCD(Grand central Dispatch:宏大的中央调度)        1) 是用纯C语言实现的.提 ...

  5. iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组

    一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数   GCD的优势 G ...

  6. IOS GCD使用实例大全

    GCD是大家在IOS开发过程中经常使用的一种多线程管理机制.原理这里就不多说了,大家关心的大部分都是它的使用,下面主要介绍GCD的主要方法及其实例. 1.认识主队列,感受串行队列的运行,运行结果打印的 ...

  7. iOS多线程GCD的使用

    1. GCD 简介 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Di ...

  8. GCD(III)

    GCD 线程间的通信 在iOS开发过程中,我们一般在主线程里边进行UI刷新,例如:点击.滚动.拖拽等事件.我们通常把一些耗时的操作放在其他线程,比如说图片下载.文件上传等耗时操作.而当我们有时候在其他 ...

  9. iOS 多线程:『GCD』详尽总结

    本文用来介绍 iOS 多线程中 GCD 的相关知识以及使用方法.这大概是史上最详细.清晰的关于 GCD 的详细讲解+总结的文章了.通过本文,您将了解到: 1. GCD 简介 2. GCD 任务和队列 ...

随机推荐

  1. js页面刷新之实现框架内外刷新(整体、局部)

    这次总结的是框架刷新: 框架内外的按钮均可以定义网页重定向, 框架内部页面的按钮可以实现局部刷新, 框架外部页面的按钮可以实现整页刷新. 代码如下(两个html页面): <!--主界面index ...

  2. oracle表分区详解

    原文来自:http://www.cnblogs.com/leiOOlei/archive/2012/06/08/2541306.html oracle表分区详解 从以下几个方面来整理关于分区表的概念及 ...

  3. linux服务器默认连接数配置

    vi /etc/security/limits.d/90-nproc.conf * - nofile 65536* - nproc 65536root soft nproc unlimited vi ...

  4. HDU 4048 Zhuge Liang's Stone Sentinel Maze

    Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/327 ...

  5. Shell基本功能&配置&Oh-My-Zsh

    http://blog.csdn.net/yangcs2009/article/details/45720193

  6. logstash filter grok 用法

    在elk+filebeat都安装好,且明白了基本流程后,主要的就是写logstash的filter了,以此来解析特定格式的日志 logstash的filter是用插件实现的,grok是其中一个,用来解 ...

  7. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

  8. 提取data.frame中的部分数据(不含列标题和行标题)

    ?unlist     Given a list structure x, unlist simplifies it to produce a vector which contains all th ...

  9. CSS布局基础之一设备像素,设备独立像素,设备像素比,css像素之间的关系

    设备像素dp(device pixels) ppi(pixels per inch)表示每英寸所拥有的像素(pixel)数目,数值越高,代表屏幕能以更高的密度显示图像. 计算公式:ppi=像素数量/物 ...

  10. 单据状态BE构建

    这节主要罗列出单据状态BE构建步骤1.创建单据状态BE实体项目,修改命名空间 2.如下图所示,分别设置实体枚举状态值 3.修改单据基本属性 构造后,至此单据状态BE构建完毕