白羽
2018-06-13
来源 :网络
阅读 875
评论 0
摘要:本文将带你了解IOS开发之NSThread、NSOperation、NSInvocationOperation的操作方法,希望本文对大家学IOS有所帮助
IOS中支持多线程操作,使用NSThread和NSInvocationOperation可以完成多线程功能。多线程的功能主要是为了防止阻塞主线程的工作(主要是UI操作和显示),使一些耗时的的操作在另一个线程中完成,完成后可以通知主线程来进行UI上的更新。多线程功能在实际开发中用的很多,最典型的就是网络请求和处理操作,下面主要来讨论一下Cocoa中的NSThread和NSInvocationOperation:
一、NSThread
创建NSThread主要有两种方式:
1.使用类方法创建
[NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];
2.使用传统方式创建
NSThread *thread = [[NSThreadalloc]initWithTarget:self selector:@selector(doInBackgroud)object:nil];
[thread start];
两种方式的区别:
1.第一种方式会立即调用并执行线程,第二种必须调用start方法后才会开始执行线程,在此之前可以对线程进行一些设置,比如线程优先级等。第二种方式与Java中线程的使用类似。
2.使用类方法(Convenient Method)创建的线程不需要进行内存清理,而使用initWithTarget方法创建的线程需要当retainCount为0时调用release方法释放内存。
//在另一个线程中运行的方法
-(void)doInBackgroud
{
NSAutoreleasePool *releasePool = [[NSAutoreleasePoolalloc]init];
//do someting...
[releasePool release];
}
多线程中执行的方法必须自行进行内存管理,否则会出现警告信息。
运行程序可以看到打印信息:
2012-08-08 11:03:21.470 ThreadTest[518:f803] Thread is start...
2012-08-08 11:03:21.471 ThreadTest[518:1291b] Thread is running...
2012-08-08 11:03:24.471 ThreadTest[518:1291b] Thread is done…
完整代码如下:
[cpp] view plain copy
1. - (void)viewDidLoad
2. {
3. [superviewDidLoad];
4.
5. NSLog(@"Thread is start...");
6. //使用类方法创建线程
7. // [NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];
8.
9. //使用传统方式创建
10. NSThread *thread = [[NSThreadalloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
11. [thread start];
12. [thread release];
13. }
14.
15. //在另一个线程中运行的方法
16. -(void)doInBackgroud
17. {
18. NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
19.
20. //do someting...
21. NSLog(@"Thread is running...");
22. [NSThread sleepForTimeInterval:3];
23. NSLog(@"Thread is done...");
24.
25. [releasePool release];
26. }
二、NSOperation
NSOperation是Cocoa中的一个抽象类,用来封装单个任务和代码执行一项操作,由于是抽象类,所以不能直接实例化使用,必须定义子类继承该抽象类来实现,比较常用的NSOperation的子类有NSInvocationOperation,另外,也可以自己继承NSOperation来实现线程的操作。
另外会使用到操作队列NSOperationQueue,它相当于一个线程队列或者可以叫做线程池,可以顺序执行队列中的操作,也可以设置队列中操作的优先级。
.h头文件
[cpp] view plain copy
1. #import <Foundation/Foundation.h>
2.
3. @interface MyTaskOperation : NSOperation
4.
5. @end
.m实现文件:
[cpp] view plain copy
1. #import "MyTaskOperation.h"
2.
3. @implementation MyTaskOperation
4.
5. //相当于Java线程中的run方法
6. -(void)main
7. {
8. //do someting...
9. NSLog(@"Thread is running...");
10. [NSThreadsleepForTimeInterval:3];
11. NSLog(@"Thread is done...");
12. }
13. @end
使用方法如下:
[cpp] view plain copy
1. //使用NSOperation子类来创建线程
2. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
3. MyTaskOperation *myTask = [[MyTaskOperation alloc] init];
4. [operationQueue addOperation:myTask];
5. [myTask release];
6. [operationQueue release];
运行结果和上面结果一样。
三、NSInvocationOperation
NSOperation的子类NSInvocationOperation提供了一套简单的多线程编程方法,是IOS多线程编程中最简单的一种实现方式。直接看代码:
[cpp] view plain copy
1. //创建操作队列
2. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
3. //设置队列中最大的操作数
4. [operationQueue setMaxConcurrentOperationCount:1];
5. //创建操作(最后的object参数是传递给selector方法的参数)
6. NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
7. //将操作添加到操作队列
8. [operationQueue addOperation:operation];
9. [operation release];
10. [operationQueue release];
运行输出结果跟上面的一样。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号