佇列的優先順序

2021-08-26 01:48:30 字數 1410 閱讀 9212

假設現在佇列中有多個操作(nsoperation),現在再向佇列中加入新的操作,並且希望新增加的操作排在未執行操作的最前面。這時就需要設定操作的優先順序了。

編寫了個最簡單的nsoperation,標頭檔案:

#import

@inte***ce myoperation : nsoperation

@property (nonatomic,retain) nsstring *name;

@end

實現檔案:

#import "myoperation.h"

@implementation myoperation

@synthesize name;

-(void) main

@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)之前執行了。

佇列 優先順序佇列

優先順序佇列的隊尾是不需要改變的,永遠在低下標處。當佇列增加資料時,隊頭的位置就是資料項的大小減去1.public class priorityq 插入 public void insert long item else quearray j 1 item nitem 刪除 public long ...

優先順序佇列

分為最小優先順序佇列和最大優先順序佇列。優先順序佇列是一種用來維護一組元素構成的集合s的資料結構,這一組元素都有乙個關鍵字key,乙個最大優先順序佇列支援的操作 insert s,x 把x插入到集合s中 maxmum s 返回s中最大元素 extra max s 去掉s中最大關鍵字並返回該最大關鍵子...

優先順序佇列

1 include stdafx.h 2 include3 4 using namespace std 5 6 define max heap len 107 int heap max heap len 8 int heap size 0 the number of elements in heap...