IOS中支持多线程操作,使用NSThread和NSInvocationOperation可以完成多线程功能。多线程的功能主要是为了防止阻塞主线程的工作(主要是UI操作和显示),使一些耗时的的操作在另一个线程中完成,完成后可以通知主线程来进行UI上的更新。多线程功能在实际开发中用的很多,最典型的就是网络请求和处理操作,下面主要来讨论一下Cocoa中的NSThread和NSInvocationOperation:
一、NSThread
创建NSThread主要有两种方式:
1.使用类方法创建
[NSThreaddetachNewThreadSelector:@selector(doInBackgroud) toTarget:selfwithObject:nil];
2.使用传统方式创建
NSThread *thread = [[NSThreadalloc]
initWithTarget:selfselector:@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…
完整代码如下:
- (void)viewDidLoad
{
[superviewDidLoad];
NSLog(@"Thread is start...");
//使用类方法创建线程
// [NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];
//使用传统方式创建
NSThread *thread = [[NSThreadalloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
[thread start];
[thread release];
}
//在另一个线程中运行的方法
-(void)doInBackgroud
{
NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
//do someting...
NSLog(@"Thread is running...");
[NSThread sleepForTimeInterval:3];
NSLog(@"Thread is done...");
[releasePool release];
}
二、NSOperation
NSOperation是Cocoa中的一个抽象类,用来封装单个任务和代码执行一项操作,由于是抽象类,所以不能直接实例化使用,必须定义子类继承该抽象类来实现,比较常用的NSOperation的子类有NSInvocationOperation,另外,也可以自己继承NSOperation来实现线程的操作。
另外会使用到操作队列NSOperationQueue,它相当于一个线程队列或者可以叫做线程池,可以顺序执行队列中的操作,也可以设置队列中操作的优先级。
.h头文件
#import <Foundation/Foundation.h>
@interface MyTaskOperation : NSOperation
@end
.m实现文件:
#import "MyTaskOperation.h"
@implementation MyTaskOperation
//相当于Java线程中的run方法
-(void)main
{
//do someting...
NSLog(@"Thread is running...");
[NSThreadsleepForTimeInterval:3];
NSLog(@"Thread is done...");
}
@end
使用方法如下:
//使用NSOperation子类来创建线程
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
MyTaskOperation *myTask = [[MyTaskOperation alloc] init];
[operationQueue addOperation:myTask];
[myTask release];
[operationQueue release];
运行结果和上面结果一样。
三、NSInvocationOperation
NSOperation的子类NSInvocationOperation提供了一套简单的多线程编程方法,是IOS多线程编程中最简单的一种实现方式。直接看代码:
//创建操作队列
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//设置队列中最大的操作数
[operationQueue setMaxConcurrentOperationCount:1];
//创建操作(最后的object参数是传递给selector方法的参数)
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
//将操作添加到操作队列
[operationQueue addOperation:operation];
[operation release];
[operationQueue release];
运行输出结果跟上面的一样。
欢迎关注我的新浪微博和我交流:@唐韧_Ryan
分享到:
相关推荐
多线程加载图片
iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
iOS多线程开发一 使用NSThread NSThread的基本使用 // demo说明 NSThreadDemoOne: 简单使用多线程,区分有多线程和没有多线的区别 NSThreadDemoTwo: 因为NSThread只能传一个一个参数,如果咬传递多个参数,使用封装...
关于iOS 多线程编程知识点,iOS 多线程 NSThread NSOperation GCD
iOS多线程开发NSThread.pdf
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
ios多线程编程指南,包含NSThread,CGD等,深入简出讲解ios多线程原理和应用
iOS多线程,四种实现方式中最简单的两种pthread,NSThread的用法介绍。
#iOS的三种多线程技术 和特性 1.NSThread 2.NSOperation/NSOperationQueue 3.GCD (大中心调度)
在iOS开发中,多线程的实现方式主要有三种,NSThread、NSOperation和GCD,我前面博客中对NSOperation和GCD有了较为详细的实现,为了学习的完整性,今天我们主要从代码层面来实现NSThread的使用。案例代码上传至 ...
2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD 全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用) 这三种编程方式从上到下,抽象度...
iphone开发之多线程NSThread和NSInvocationOperation 的 使用
IOS多线程编程NSThread的使用方法 NSThread是多线程的一种,有两种方法创建子线程 (1)优点:NSThread 比GCD、NSOperation都轻量级 (2)缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有...
下面小编就为大家分享一篇iOS NSThread和NSOperation的基本使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这里详细介绍了iOS多线程 NSThread,GCD,NSOperation的使用。看我就够了!
ios 开辟分线程,通过nsthread开辟了一个新的线程
IOS应用源码——ui多线程的简单例子 NSThread.zip
那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了。 上代码 声明变量 @interface ViewController () @property (strong, nonatomic)NSThread *thread1; @property (strong, nonatomic)...
在app store中的很多应用程序非常...然而,幸运的是apple从os x10.5在这方面做了很多的改进,NSThread的引入,使得开发多线程应用程序容易多了。除此之外,它们还引入了两个全新的类,NSOperation和NSOperationQueue。