博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
队列的优先级
阅读量:6949 次
发布时间:2019-06-27

本文共 1221 字,大约阅读时间需要 4 分钟。

在中介绍了iOS的操作队列使用方法。这里补充一下队列的优先级功能。

假设现在队列中有多个操作(NSOperation),现在再向队列中加入新的操作,并且希望新增加的操作排在未执行操作的最前面。这时就需要设置操作的优先级了。

编写了个最简单的NSOperation,头文件:

#import <Foundation/Foundation.h>

@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://dshnl.baihongyu.com/

你可能感兴趣的文章
矩阵快速幂 学习笔记
查看>>
linux iconv 批量转码
查看>>
使用MongoDB的GridFS保存用户文件的折腾日记
查看>>
Linux的Find使用
查看>>
ios开发工程师笔试基础题
查看>>
基于Struts构建新闻发布系统
查看>>
基于Struts实现用户登录和注册模块
查看>>
CentOS安装Apache
查看>>
C++ getline函数的使用
查看>>
SQL Server删除重复行的6个方法
查看>>
Mysql 临时表的创建和删除
查看>>
db file scattered read等待事件
查看>>
ArcGIS Engine 中的多线程使用 (转载)
查看>>
linux下c的网络编程---转载
查看>>
filter中的DelegatingFilterProxy使用事例
查看>>
WinForm 天猫2013双11自动抢红包【源码下载】
查看>>
学习数学从《数学之美》开始
查看>>
flashcache的实现与分析
查看>>
[UML]UML系列——状态机图statechart diagram
查看>>
微信公众平台开发(74) 用户分组管理
查看>>