NSOperation和NSOperationQueue
来源:互联网 发布:java创建文件夹和文件 编辑:程序博客网 时间:2024/05/16 09:06
NSOperation使用需要继承它,用子类重写main方法,也可以使用它已有的两个子类:
//target添加 NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(calculate) object:self]; ...operation2 ...operation3//block实现NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ [self calculate]; }];
设置依赖关系(1,2 都先执行完再执行3)
[operation3 addDependency:operation1];[operation3 addDependency:operation2];
设置优先级<<先满足依赖关系>>
operation1.queuePriority = NSOperationQueuePriorityHigh;operation2.queuePriority = NSOperationQueuePriorityNormal;优先级不能替代依赖关系,优先级只是对已经准备好的 operations确定执行顺序。<<先满足依赖关系>>,然后再根据优先级从所有准备好的操作中选择优先级最高的那个执行。
启动线程
//手动启动 start 默认是同步的;先启动则先执行(1,2 达不到并行) [operation1 start]; [operation2 start]; [operation3 start];//放在队列中自动启动 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:operation1]; [queue addOperation:operation2]; [queue addOperation:operation3];//设置并发数queue.maxConcurrentOperationCount = 1;(没有设置依赖关系和优先级的时候,并且最大并发数为1,会根据添加的顺序去执行这个只是自己试验多次结果都一样,不知道是否一定准确)设置并发数为1后,会根据优先级执行可能先执行了优先级高的,然后优先级低的在执行,只是在执行任务的时(此处的任务都是打印),打印的时候优先级低的先打印了。但是执行的时候还是先执行优先级高的,如果并发数不是1,然后并发执行(所以可以通过设置最大并发数为1,来查看先执行的哪个);
取消Operations
一旦添加到队列,queue就拥有了这个Operation对象并且不能被删除唯一能做的事情是取消// 取消单个操作 [operation1 cancel]; // 取消queue中所有的操作 [queue cancelAllOperations];
暂停和继续queue
// 暂停queue [queue setSuspended:YES]; // 继续queue [queue setSuspended:NO]; 如果你想临时暂停Operations的执行,可以使用上面方法;不过暂停一个queue不会导致正在执行的operation在任务中途暂停,只是简单地阻止调度新Operation执行。你可以在响应用户请求时,暂停一个queue来暂停等待中的任务。稍后根据用户的请求,可以再次条用,继续queue中operation的执行
0 0
- Cocoa:NSOperation和NSOperationQueue
- Cocoa:NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- Cocoa: NSOperation和NSOperationQueue
- Cocoa:NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- Cocoa:NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- Cocoa:NSOperation和NSOperationQueue
- NSOperation和NSOperationQueue
- NSOperationQueue和NSOperation
- NSOperation 和 NSOperationQueue
- NSOperation和NSOperationQueue
- 231,NSOperation和NSOperationQueue
- 多线程NSOperation和NSOperationQueue
- 类中用于比较两对象的方法的原型 及ios_base用法
- 笔试题做题笔记
- php设计模式入门-注册表模式
- 无限互联学习心得(三)
- vector\list\deque的选取
- NSOperation和NSOperationQueue
- php dirname(__FILE__) 获取当前文件的绝对路径
- 4K 蓝光影片规范出炉,定名 Ultra HD Blu-ray
- C语言通过指针引用数组
- PTAM编译选项
- hdu 1166
- C#关于使用正则表达式解析字符串
- UILabel
- The Dole Queue