NSOperationQueue API
来源:互联网 发布:淘宝类目详细划分 编辑:程序博客网 时间:2024/06/07 11:05
1、 iOS最常用的线程有以下三种,
1).、NSThread
2)、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)
3)、GCD 全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用)
这三种编程方式从上到下,抽象度层次是从低到高的,抽象度越高的使用越简单,也是Apple最推荐使用的。
NSThread
NSThread 能创建一个轻量级线程,但也是使用起来最负责的,你需要自己管理thread的生命周期,线程 之间的同步。线程 共享同一应用程序的部分内存空间,它们拥有对数据相同的访问权限。 你得协调多个线程 对同一数据的访问,一般做法是在访问之前加锁,这会导致一定的性能开销。在 iOS 中我们可以使用多种形式的 thread:
创建与启动NSthread线程 NSThread的创建主要有以下方式, 以下方法都可带有一个参数,不用时只需要设为nil就行。
1)、立即创建一个线程来做事情,马上运行。
[NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:nil];
2)、调用alloc创建一个线程,但不会马上执行,需要我信手动调用 start 启动线程时才会真正去创建线程,需要自己管理内存
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:@selector(downloadImage:)
object:nil];
//调用 start 启动线程
[myThread start];
3)、用NSObject的类方法创建一个线程,马上运行。
[NSObject performSelectorInBackground:@selector(downloadImage:) withObject:nil];
//线程调用的方法,
(void)downloadImage:(NSString *)ic{
//多线程中一定要有自己的释放池,否则内存得不到释放
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
if(图片为空){
下载图片过程
}
//调用performSelectorOnMainThread:withObject: waitUntilDone:方法回到主线程
[self performSelectorOnMainThread:@selector(updateImage) withObject:ic waitUntilDone:YES];[pool release];
}
线程间通信
线程 在运行过程中,可能需要与其它线程 进行通信。我们可以使用 NSObject 中的一些方法:
1)回到主线程中做事情:
performSelectorOnMainThread:withObject:waitUntilDone:
performSelectorOnMainThread:withObject:waitUntilDone:modes:
2)在指定线程中做事情:
performSelector:onThread:withObject:waitUntilDone:
performSelector:onThread:withObject:waitUntilDone:modes:
3)在当前线程中做事情:
performSelector:withObject:afterDelay:
performSelector:withObject:afterDelay:inModes:
4)取消发送给当前线程的某个消息
cancelPreviousPerformRequestsWithTarget:
cancelPreviousPerformRequestsWithTarget:selector:object:
- NSOperationQueue [API]
- NSOperationQueue API
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationqueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue简介
- 关于 NSOperationQueue
- NSOperationQueue小结
- NSOperationQueue应用
- NSOperationQueue 队列
- EditText编辑框
- 百度地图4.1_1开发教程(7)截图工具类
- 开源客户端框架:JavaScript
- JXL读取Excel日期时间不准确
- hwcomposer模块的接口
- NSOperationQueue API
- Oracle 存储过程 与 函数 区别
- int、long、NSInterger、NSUInterger、NSNumber 区别和使用
- jQuery遍历li节点
- 第十四周课后作业——项目一(1)
- POJ 1995Raising Modulo Numbers
- 图形处理之 OpenGL
- 创建csv文件实例
- 自定义圆形ImageView