OC 线程操作 - GCD使用 -线程通讯, 延迟函数和一次性代码
- (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使用 -线程通讯, 延迟函数和一次性代码的更多相关文章
- OC 线程操作 - GCD队列组
1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...
- OC 线程操作 - GCD快速迭代
- (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...
- CreateThread 线程操作与 _beginthreadex 线程安全(Windows核心编程)
0x01 线程的创建 线程不同于进程,Windows 中的进程是拥有 '惰性' 的,本身并不执行任何代码,而执行代码的任务转交给主线程,列如使用 CreateProcess 创建一个进程打开 Cmd ...
- OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...
- OC线程操作-GCD介绍
1. GCD介绍 1.11.2 1.3 异步具备开启能力但是不是 一定可以开启 1.4 1.5 67. 8.
- OC 线程操作 - GCD使用 - 栅栏函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...
- OC 线程操作2 - NSThread
方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...
- ios线程和GCD
1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...
- 创建线程方式-GCD
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
随机推荐
- mysql存储过程的参数名不要跟字段名一样 (血淋淋的代价)
如题,将会导致的结果就是参数的值将不会是你传入的值,而是变成每条记录的那个字段的值. 这样的后果,是灰常严重的.比如执行删除操作,它能把整个表的记录全删了. 这个是我的血淋淋的代价啊. 死坑如下,勿跳 ...
- ECCV 2018 | Bi-Real net:超XNOR-net 10%的ImageNet分类精度
这项工作由香港科技大学,腾讯 AI lab,以及华中科技大学合作完成,目的是提升二值化卷积神经网络(1-bit CNN)的精度.虽然 1-bit CNN 压缩程度高,但是其当前在大数据集上的分类精度与 ...
- Python之函数——内置函数
内置函数(Built-in Functions) 截止到3.6版本,python一共为我们提供了68个内置函数.它们就是python提供给的可以直接拿来使用的所有函数,接下来让我们一起认识一下这些函数 ...
- springboot-dokcer
项目就一个java文件,仅用于样例 package com.example.demo; import org.springframework.beans.factory.annotation.Valu ...
- kvm安装及使用
****centos7安装及使用kvm: http://blog.csdn.net/github_27924183/article/details/76914322?locationNum=5& ...
- angularjs 整合 bootstrap
第一步 :下载 bootstrap jquery ppper.js npm install bootstrap@4.0.0-beta.2 jquery popper.js --save 第二步: ...
- jackson的小知识
- java 项目中类找不到异常解决办法
最后点击Apply and Close就可以了
- XE 创建 Active Form
XE6: http://docwiki.embarcadero.com/RADStudio/XE6/en/Generating_an_Active_Form_Based_on_a_VCL_Form h ...
- Ping ip能ping通,ping计算机名ping不通,网络共享不能访问
名称 协议 端口 NetBIOS Name Service UDP 137 NetBIOS Datagram Service UDP 138 NetBIOS Session Service TCP 1 ...