iOS 多线程编程

NSOperationQueue最大并发量、暂停、取消

需要注意的是,当使用暂停、取消任务时,需要等到正在执行的任务执行完成后,才会执行这个动作。其中取消任务的动作不可逆,取消之后任务不能重新开启。具体代码如下:
//
//  ViewController.m
//  NSOperation
//
//  Created by fe on 2016/10/19.
//  Copyright © 2016年 fe. All rights reserved.
//

#import "ViewController.h"
#import "ZYFOperation.h"
@interface ViewController ()
@property (nonatomic,strong) NSOperationQueue *operationQueue;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self operationQueueMaxConcurrentOperationCount];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //当self.operationQueue.suspended 为YES时执行任务,为NO时暂停任务
    //每个任务都有执行状态,暂停的时候正在执行的任务执行完,才会暂停
    //self.operationQueue.suspended = !self.operationQueue.suspended;
    
    //取消所有的任务,正在执行的任务执行完,才会执行这个动作,和暂停类似。该方法不可逆,取消后不能恢复执行
    [self.operationQueue  cancelAllOperations];
}
//NSOperationQueue,最大并发数
- (void)operationQueueMaxConcurrentOperationCount
{
    //1:创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    /*
     默认是并发队列,如果最大并发数>1,并发
     如果最大并发数==1,串行
     系统默认的并发数是-1,所有任务全部并发执行
     */
    queue.maxConcurrentOperationCount = 1;
    //2:封装操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"1------%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"2------%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"3------%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"4------%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"5------%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op6 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"6------%@",[NSThread currentThread]);
    }];
    
    //3:把操作添加到队列
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];
    [queue addOperation:op5];
    [queue addOperation:op6];
    self.operationQueue = queue;
}
@end

当使用自定义类继承自NSOperation时,官方建议使用判断语句,当在执行完一小段耗时操作后,判断一下是否执行了取消操作,如果执行了取消操作,就取消任务。具体代码入下:
//
//  ZYFOperation.m
//  NSOperation
//
//  Created by fe on 2016/10/19.
//  Copyright © 2016年 fe. All rights reserved.
//

#import "ZYFOperation.h"

@implementation ZYFOperation
- (void)main
{
    for (NSInteger i = 0; i<10000; i++) {
        
        NSLog(@"1----%zd----%@",i,[NSThread currentThread]);
    }
    if (self.isCancelled) {
        return;
    }
    for (NSInteger i = 0; i<10000; i++) {
        
        NSLog(@"2----%zd----%@",i,[NSThread currentThread]);
    }
    
    if (self.isCancelled) {
        return;
    }
    for (NSInteger i = 0; i<10000; i++) {
        
        NSLog(@"3----%zd----%@",i,[NSThread currentThread]);
    }
}
@end



你可能感兴趣的:(iOS开发)