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 ...
随机推荐
- GNS3 IOU 配置
GNS3使用视频: http://edu.51cto.com/lesson/id-25295.html GNS3 IOU 与VM http://www.mamicode.com/info-detail ...
- could not read symbols: File format not recognized
arm-linux-gnueabi-readelf工具解决问题 编译一个32位平台的内核时,出现如下错误提示: libschw.a: could not read symbols: File form ...
- 本机不能访问虚拟机中flask的web服务器的解决办法
在虚拟机的linux里写flask程序,在本机中用"ip:端口号"的方法不能访问,查资料试了各种方法都不行.最后发现问题出在flask程序本身: flask的app.run()方法 ...
- Matrix
记载: Matrix Matrix是Android 提供的一个矩阵工具类,位于"android.graphics.Matrix"包下,它本身不能对图像或View进行变换, 但它可以 ...
- 实验三——for 语句及分支结构else-if
1.本节课学习到的知识点:在本次课中,我学习了for语句的使用,认识了for语句的执行流,明确了三种表达式的意义.以及最常用的实现多分支的else-if语句. 2.实验过程中遇到的问题及解决方法:在本 ...
- 收藏夹里的js
释放右键 javascript:(function(){var doc=document;var bd=doc.body;bd.onselectstart=bd.oncopy=bd.onpaste=b ...
- php时间设置为本地
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to ...
- js 判断是什么类型浏览器
// firefoxif ( window.sidebar && "object" == typeof( window.sidebar ) && ...
- MySQL数据库的一些基本操作及注释
--Created by mac on 2017/1/4. -- MySQL数据库 -- ****************** 一. 连接数据库服务器的基础命令 ******************* ...
- MSDN Library for vs 2010安装及使用(MSDN Library)
VS2010正式版不再有单独的MSDN Library安装选项,VS2010的ISO安装光盘里已经包含有MSDN Library,只不过要手动安装,方法如下: 1.安装完VS2010后,在开始菜单中打 ...