- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// [self downImage];
// [self delay];
[self once];
[self once];
[self once];
} /**
一次性代码: 整个应用程序只会执行一次
不可以放在懒加载里面, 如果在类中使用一次性代码, 创建的第二个对象, 就完全不会初始化,
static : 全局变量
*/
-(void)once{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"---once---");
});
} /**
常用函数 : 延迟方法
*/
-(void)delay{
NSLog(@"----start----"); /**
参数1:(nonnull SEL) 方法名字
参数2:(nullable id) 传递参数
参数3:(NSTimeInterval) 时间 */
//1. 只在主线程执行
[self performSelector:@selector(task) withObject:nil afterDelay:]; //2.定时器方法 只在主线程执行
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(task ) userInfo:nil repeats:YES]; //3.GCD 可以在主线程和子线程执行
/**
参数1:DISPATCH_TIME_NOW 现在开始计算时间
参数2:延迟时间 2.0 GCD 时间单位:纳秒 NSEC_PER_SEC 1000000000ull
参数3:(NSTimeInterval) 时间
*/ // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self task];
// });
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
[self task];
}); dispatch_queue_t global_queue = dispatch_get_global_queue(, );
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), global_queue, ^{
[self task];
}); } - (void)task{
NSLog(@"---task---%@", [NSThread currentThread]);
} //线程间通讯 - 各种嵌套
-(void)downImage{ //1. 创建异步函数, 这里只有一个任务,用并发和串行都可以
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
// });
dispatch_async(dispatch_get_global_queue(, ), ^{ //1.1确定URL
NSURL *url = [NSURL URLWithString:@"http://www.leawo.cn/attachment/201309/4/756352_1378261981fNNT.png"]; //1.2下载二进制数据到本地
NSData *data = [NSData dataWithContentsOfURL:url]; //1.3转换图片
UIImage *image = [UIImage imageWithData:data]; // [self.iv performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; // [self.iv performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO]; // dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"=====%@", [NSThread currentThread]);
// self.iv.image = image;
// }); //这里不会死锁, 因为这个任务是在子线程里 创建的,
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"=====%@", [NSThread currentThread]);
self.iv.image = image;
}); }); }

OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码的更多相关文章

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

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

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

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

  3. CreateThread 线程操作与 _beginthreadex 线程安全(Windows核心编程)

    0x01 线程的创建 线程不同于进程,Windows 中的进程是拥有 '惰性' 的,本身并不执行任何代码,而执行代码的任务转交给主线程,列如使用 CreateProcess 创建一个进程打开 Cmd ...

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

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

  5. OC线程操作-GCD介绍

    1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.

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

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

  7. OC 线程操作2 - NSThread

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

  8. ios线程和GCD

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

  9. 创建线程方式-GCD

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. thinkphp 5.0手记

    场景配置,可配置多个数据库,按需求加载 数组合并:array_merge();键名相同后面覆盖前面 array_merge_recursive();键名相同,键值合并 对与http://localho ...

  2. javascript创建对象之动态原型模式(五)

    function Human(name, sex) { this.name = name; this.sex = sex; if (typeof this.say != "function& ...

  3. AVL树Python实现(使用递推实现添加与删除)

    # coding=utf-8 # AVL树的Python实现(树的节点中包含了指向父节点的指针) def get_height(node): return node.height if node el ...

  4. awk(gawk)

    awk,逐行处理文本内容.Linux里的awk其实是“gawk”. 使用格式: awk [选项] '模式匹配 {命令 命令参数}' file1, file2, …… 支持的选项 说明 -f progr ...

  5. 9. PD逆向工程--由数据库转为模型(ER图)

    步骤: 1. 在控制面板-->管理工具(如果没找到管理工具,查看方式改为大图标)-->数据源(ODBC)-->用户DSN -->用户数据源下添加一个数据源(这里根据情况添加数据 ...

  6. nginx直接返回json

    尝试配置nginx.conf之后,访问直接变成下载文件... 查阅之后,发现需要配置返回内容的格式. location ~ ^/get_json { default_type application/ ...

  7. delphi c++builder JSON 生成与解析 例子

    json,System.JSON,REST.JSON JSON有两种数据结构,对象和数组. 对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...} 数组 ...

  8. php iconv 函数

    原型: $txtContent = iconv("utf-8",'GBK',$txtContent); 特殊参数:iconv("UTF-8","GB2 ...

  9. webserive学习记录5-拦截器完成登陆校验

    说说cxf中的拦截器,可以分为系统拦截器(如日志拦截器)和自定义拦截器,也可以分为出拦截器和入拦截器,也可以分为服务器拦截器和客户端拦截器. 下面将实现一个可以进行登陆验证的拦截器,其中用户名作为方法 ...

  10. Linux逻辑卷的拉伸和缩小

    相对于普通磁盘优势是逻辑卷可以在线动态的拉伸(不用先unmount操作),只要vg中有足够的空间即可 1:首先查看vg是否有足够的空间 [root@gechong mapper]# vgdisplay ...