您的位置:首页 > 产品设计 > UI/UE

iphone 开发NSOperation和NSOperationQueue

2011-08-22 14:53 363 查看
@interface MyTask : NSOperation { //继承NSOperation

int operationId;

}

@property int operationId;

@end

@implementation MyTask

@synthesize operationId;

//main函数就是线程里要执行的东西

-(void)main{

NSLog(@"task %i run...",operationId);

[NSThread sleepForTimeInterval:10];

[viewController log];

NSLog(@"task %i is finished.",operationId);

}

@end

- (void)viewDidLoad {

[super viewDidLoad];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

int index = 1;

MyTask *task = [[[MyTask alloc] init] autorelease];

task.viewController = self;

task.operationId = index++;

[queue addOperation:task]; //把task加到NSOperationQueue就会执行线程

task = [[[MyTask alloc] init] autorelease];

task.operationId = index++;

[queue addOperation:task];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐