NSOperationQueue的简单应用
来源:互联网 发布:javiewer差不多的软件 编辑:程序博客网 时间:2024/05/29 13:14
NSOperationQueue的简单应用
在使用NSOperationQueue简化多线程开发中介绍了iOS的操作队列使用方法。这里补充一下队列的优先级功能。
假设现在队列中有多个操作(NSOperation),现在再向队列中加入新的操作,并且希望新增加的操作排在未执行操作的最前面。这时就需要设置操作的优先级了。
编写了个最简单的NSOperation,头文件:
import
@interface MyOperation : NSOperation{
NSString *name;
}
@property (nonatomic,retain) NSString *name;
@end
实现文件:
import “MyOperation.h”
@implementation MyOperation
@synthesize name;
-(void) main{
NSLog(@”run operation: %@”,name);
[NSThread sleepForTimeInterval:3];
}
@end
在控制器里创建3个实例:
- (void)viewDidLoad {
[super viewDidLoad];
queue=[[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
MyOperation *o1=[[[MyOperation alloc] init] autorelease];
o1.name=@”o1”;
[queue addOperation:o1];
MyOperation *o2=[[[MyOperation alloc] init] autorelease];
o2.name=@”o2”;
[queue addOperation:o2];
[NSThread sleepForTimeInterval:1];
MyOperation *o3=[[[MyOperation alloc] init] autorelease];
o3.name=@”o3”;
[o3 setQueuePriority:NSOperationQueuePriorityHigh];
[queue addOperation:o3];
运行效果:
2011-07-26 14:47:23.822 NSOperationQueueDemo[27495:6003] run operation: o1
2011-07-26 14:47:26.823 NSOperationQueueDemo[27495:6003] run operation: o3
2011-07-26 14:47:29.824 NSOperationQueueDemo[27495:6203] run operation: o2
可见,操作3成功的插到第一个等待操作(操作2)之前执行了
转载自:http://blog.sina.com.cn/s/blog_5fb39f910101dhh0.html
- NSOperationQueue的简单应用
- NSOperationQueue的简单应用
- NSOperationQueue应用
- 关于GCD/NSThread/NSOperationQueue的简单使用
- nsoperationQueue的使用
- NSOperationQueue队列的优先级
- iOS NSOperationQueue的使用
- NSOperationQueue队列的特点
- NSOperationQueue 的NSOperation 数
- 关于NSOperationQueue的cancel
- NSOperationQueue 的 cancelAllOperations
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- NSOperationQueue
- 初涉WebView编程
- Android笔记(三)杂记待续
- linux编译过程
- 一、Shiro+Spring+Struts2集成演示权限控制Demo示例
- Android getLeft
- NSOperationQueue的简单应用
- Gas Station
- AOP OOP介绍
- Python Tutorial(六):模块
- Qt5.1 发布程序
- Android开发学习笔记之sdk更新问题
- Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍
- sas中retain语句的作用
- 在网络营销的产品在许多方面