IOS线程学习(一)
1.NSThread
官方的描述
An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application.
NSThread能控制一个线程的执行, 当你想在自己的线程执行OC方法时请用此类。对于执行较长的任务时这是也很有用的,不会堵住程序里面剩下需要执行的任务。
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:NSSelectorFromString(@"myThread:") object:nil];
//启动线程
[thread start];
//停止线程
//if (![thread isCancelled]) {
//[thread cancel];
// }
-(void)myThread:(id)sender{
NSLog(@"%@" , sender);
@synchronized(self){
while (true) {
[NSThread sleepForTimeInterval:];
static int i = ;
NSLog(@"%i" , i++);
if (i == ) {
[NSThread exit];
}
}
}
}
结果:只打印到3时线程就终止了
:
也可以用这个方法启动一个线程,但是不能是默认的Thread配置
[NSThread detachNewThreadSelector:NSSelectorFromString(@"myThread:") toTarget:self withObject:@"myThread"];
也等同于,这个方法在NSObject中被定义,只要是继承NSObject都可以这样用
[self performSelectorInBackground:NSSelectorFromString(@"myThread:") withObject:@"myThread"];
2.NSOperation
目前我的理解就是一个封装操作某操作的,然后调用其start方法,就在主线程执行!!!
如果不在主线程执行可以创建一个NSOperationQueue,然后将操作加入到其中执行
其有两个子类NSBlockOperation和NSInvocationOperation
NSBlockOperation
The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.
可见,NSBlockOperation是管理多个Block块的,而且只有所有的Block都执行完了才会变成finished状态;
NSLog(@"%@ mainT = " ,[NSThread currentThread]);
NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i<; i++) {
[NSThread sleepForTimeInterval:];
NSLog(@"block1>>>%i thread = %@" , i , [NSThread currentThread]);
}
}];
[blockOp addExecutionBlock:^{
for (int i = ; i<; i++) {
[NSThread sleepForTimeInterval:];
NSLog(@"block2>>>%i thread = %@" , i , [NSThread currentThread]);
}
}];
[blockOp start];
NSLog(@"到这了");
运行结果:一个Block就在主线程,多个就会并行执行其他block

NSInvocationOperation
The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation.
可见,只能通过Action-Target模式加入一个操作
NSLog(@"%@ mainThread = " ,[NSThread currentThread]);
NSInvocationOperation *invocationOperation1= [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myOperation:) object:@"invocationOperation1"]; [invocationOperation1 start]; NSLog(@"到这了"); -(void)myOperation:(id)sender{
static int i = ;
while (i<) {
[NSThread sleepForTimeInterval:];
[NSThread isMainThread];
NSLog(@"我是线程%@ %i", [NSThread currentThread] , i++);
}
}
运行结果:
-- ::23.038 MYThread[:] <NSThread: 0x7ffcba50c190>{number = , name = main} mainThread =
-- ::24.043 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::25.046 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::26.051 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.053 MYThread[:] 我是线程<NSThread: 0x7ffcba50c190>{number = , name = main}
-- ::27.054 MYThread[:] 到这了
3.NSOperationQueue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(method1) object:nil];
[queue addOperation:op];
NSLog(@"到这了!");
结果:
2016-03-17 09:17:22.710 ViewAnim[580:12599] 到这了!
2016-03-17 09:17:23.779 ViewAnim[580:12871] 111111
2016-03-17 09:17:24.853 ViewAnim[580:12871] 111111
2016-03-17 09:17:25.926 ViewAnim[580:12871] 111111
2016-03-17 09:17:27.001 ViewAnim[580:12871] 111111
2016-03-17 09:17:28.070 ViewAnim[580:12871] 111111
2016-03-17 09:17:29.143 ViewAnim[580:12871] 111111
。。。。。
IOS线程学习(一)的更多相关文章
- 开源中国iOS客户端学习
开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客 http://blog.csdn.net/column/details/xfzl-kykhd.html ...
- IOS基础学习-2: UIButton
IOS基础学习-2: UIButton UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...
- IOS 线程处理 子线程
IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585 IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...
- iOS阶段学习第一天笔记(Mac终端的操作)
前言部分 原本从事的是.NET开发,一直在要不要转iOS 中犹豫徘徊,经过复杂的内心挣扎终于鼓起勇气辞职脱产学习iOS;希望通过四个月的 学习后能够拿到理想的薪资.以下是学习过程中的学习笔记,为了方便 ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
- iOS之学习资源收集--很好的IOS技术学习网站
点击图片也能打开相关的网站: https://boxueio.com/skill/swift http://ios.b2mp.cn/ http://gold.xitu.io/welcome/?utm_ ...
- ios开发之OC基础-ios开发学习路线图
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
随机推荐
- STM32 串口固件库中定义的几个中断标志位什么意思?
在stm32f10x_usart.h中以上几个宏,很没有规律,诈一看还真不知道为什么会这么定义,其实通过代码就很容易明白: D7~D5:代表中断标志位对应的中断使能位在 CR1.CR2还是CR3寄存器 ...
- 还有 3 天,苹果就要关上 HTTP 大门了
版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/274113001482113656 来源:腾云阁 h ...
- IO流中SequenceInputStream类
SequenceInputStream类: 不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个In ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- Hbase关于Java常用API举例
1. HBase相关对Admin操作的的API封装在HBaseAdmin中,封装了HBase常用操作的API 使用方法: pom.xml <!-- https://mvnrepository.c ...
- chrome经常崩溃解决过程
之前chrome常崩溃,也没有找到原因,就将就着用吧,一直用到今天,今天连续几次崩了,突然想到,难道是因为我访问的域名没有解析(能想到这个,是因为今天分配公司域名测试的时候常输错),于是就输入一个不存 ...
- vs中部分快捷键
ctrl + r ctrl + r ctrl 按两次r修改变量名(修改被引用到的变量名),不同于ctrl + f(修改全部相同名字的) ctrl +r ctrl + m 先选中一部分da ...
- ionic 初入门
ionic ionic 是webapp开发的一个框架 安装 npm install -g cordova ionic ; 我这两个分开装,因为ionic模块拖不下来,所以只好等待时机.这时候科学上网 ...
- AutoCAD2006启动慢解决方案
一. 1.打开控制面板.2.选择Internet选项.3.在Internet属性对话框里,点高级标签.4.清除“检查发行商的证书吊销”选项.5.单击应用,然后单击OK.6.重新启动,运行应用程序. 二 ...
- golang使用 mongo
连接集群 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?op ...