NSOperationQueue的其他方法

Posted on 2016-07-17 17:14  柠檬片  阅读(131)  评论(0)    收藏  举报

1.设置最大并发数

  什么是并发数

  同时执行的任务数
  比如,同时开3个线程执行3个任务,并发数就是3
 
  最大并发数的相关方法

  - (NSInteger)maxConcurrentOperationCount;

  - (void)setMaxConcurrentOperationCount:(NSInteger)cnt;

 

   默认是并发队列,如果最大并发数>1,并发

     如果最大并发数==1,串行队列

     系统的默认是最大并发数-1

 1 -(void)maxConcurrentOperationCount
 2 {
 3     //1.创建队列
 4     NSOperationQueue *queue = [[NSOperationQueue alloc]init];
 5     
 6     //maxConcurrentOperationCount
 7     /*
 8      默认是并发队列,如果最大并发数>1,并发
 9      如果最大并发数==1,串行队列
10      系统的默认是最大并发数-1
11      
12      */
13     queue.maxConcurrentOperationCount = 1;
14     
15     //2.封装操作
16     NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
17         NSLog(@"1---%@",[NSThread currentThread]);
18     }];
19     
20     NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
21         NSLog(@"2---%@",[NSThread currentThread]);
22     }];
23     
24     NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
25         NSLog(@"3---%@",[NSThread currentThread]);
26     }];
27     
28     NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
29         NSLog(@"4---%@",[NSThread currentThread]);
30     }];
31     
32     NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
33         NSLog(@"5---%@",[NSThread currentThread]);
34     }];
35     
36     //3.把操作添加到队列
37     [queue addOperation:op1];
38     [queue addOperation:op2];
39     [queue addOperation:op3];
40     [queue addOperation:op4];
41     [queue addOperation:op5];
42 }
示例

 

2.队列的取消、暂停、恢复

  取消队列的所有操作(取消队列中的所有线程,是不可恢复的)

  - (void)cancelAllOperations;

  提示:也可以调用NSOperation的- (void)cancel方法取消单个操作

 

  暂停和恢复队列

  - (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列

  - (BOOL)isSuspended;

 1 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 2 {
 3 //    suspended
 4 //    self.queue.suspended = YES;
 5     //当值为YES的适合暂停,为NO的时候是恢复
 6 //    if (self.queue.suspended) {
 7 //        NSLog(@"恢复执行");
 8 //        self.queue.suspended = NO;
 9 //    }else
10 //    {
11 //        NSLog(@"暂停");
12 //        self.queue.suspended = YES;
13 //    }
14     
15     //取消,不再执行
16     [self.queue cancelAllOperations];
17     
18 }
示例

 

3.补充:如何中断线程中的操作

  队列的取消、暂停、恢复,是不能中断正在执行的操作的,只对出去等待调度队列中的线程有效.想要中断正在执行的线程,可以用以下方法:

  用self.isCancelled判断外界是否执行了中断操作,然后在调用return方法.

@implementation YSOperation
-(void)main
{
    //耗时操作
    for (NSInteger i =0; i<1000; i++) {
//        if (self.isCancelled) {
//            return;
//        }
        NSLog(@"1-%zd---%@",i,[NSThread currentThread]);
    }
    
    if (self.isCancelled) {
        return;
    }
    for (NSInteger i =0; i<1000; i++) {
        NSLog(@"2-%zd---%@",i,[NSThread currentThread]);
    }
    
    if (self.isCancelled) {
        return;
    }
    
    for (NSInteger i =0; i<1000; i++) {
        NSLog(@"3-%zd---%@",i,[NSThread currentThread]);
    }
}
@end
示例--在继承自NSOperation的类中中断线程