NSOperationQueue

NSOperationQueue

/// queue上的任务的执行进度.(执行完了才能算)
@property (readonly, strong) NSProgress *progress API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));

使用用例:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.progress.totalUnitCount = 6;
queue.maxConcurrentOperationCount = 2;
NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"1:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"2:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"3:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation4 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"4:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation5 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"5:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation6 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"6:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
[queue addOperation:blockOperation1];
[queue addOperation:blockOperation2];
[queue addOperation:blockOperation3];
[queue addOperation:blockOperation4];
[queue addOperation:blockOperation5];
[queue addOperation:blockOperation6];
[queue addBarrierBlock:^{
    NSLog(@"7:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
/// 添加操作
- (void)addOperation:(NSOperation *)op;

/// 添加操作,wait:是否等操作完成
- (void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 使用block的方式添加操作.
- (void)addOperationWithBlock:(void (^)(void))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 类似`dispatch_barrier_async`
- (void)addBarrierBlock:(void (^)(void))barrier API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));

/// queue最大并发数(1的时候是串行线程)
@property NSInteger maxConcurrentOperationCount;

/// 暂停
@property (getter=isSuspended) BOOL suspended;

/// queue的名称
@property (nullable, copy) NSString *name API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 服务质量
@property NSQualityOfService qualityOfService API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

/// 任务执行的队列.
@property (nullable, assign /* actually retain */) dispatch_queue_t underlyingQueue API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

用例:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.underlyingQueue = dispatch_get_main_queue();
/// 取消queue上的所有操作.正在执行状态的操作
- (void)cancelAllOperations;

/// 等待所有任务执行完毕(会卡住创建queue的线程)
- (void)waitUntilAllOperationsAreFinished;

/// 获取当前queue
@property (class, readonly, strong, nullable) NSOperationQueue *currentQueue API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 获取mainQueue(dispatch_get_main_queue())
@property (class, readonly, strong) NSOperationQueue *mainQueue API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

Deprecated(已废弃)

// These two functions are inherently a race condition and should be avoided if possible

/// 所有操作(addBarrierBlock代替)
@property (readonly, copy) NSArray<__kindof NSOperation *> *operations API_DEPRECATED("access to operations is inherently a race condition, it should not be used. For barrier style behaviors please use addBarrierBlock: instead", macos(10.5, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED));

/// 操作个数(使用queue.progress.completedUnitCount)
@property (readonly) NSUInteger operationCount API_DEPRECATED_WITH_REPLACEMENT("progress.completedUnitCount", macos(10.6, API_TO_BE_DEPRECATED), ios(4.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED));

你可能感兴趣的:(多线程)