NSOperation和NSOperationQueue的一些基本操作

当初学习多线程这一块的时候,时间比较匆忙,没有细细考虑,而今重新学一次,算是复习和总结吧。

 

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 
  5 @end
  6 
  7 @implementation ViewController
  8 
  9 /*
 10  通过调用[self invocationOperation]的log,可以发现,在不加入queue的情况下,默认是在主线程中同步执行操作的
 11  
 12  而通过调用[self blockOperation]的log,可以发现,在不加入queue的情况下,同一个block里的任务是,默认是在主线程中同步执行操作的。但是,如果有多个block操作,会是并发、异步执行的
 13  
 14  NSBlockOperation 可以设置监听,调用[self opeationListen]  在完成任务后,可以设置完成这个任务想要执行的操作
 15  
 16  调用[self operationQueue]的log,可以发现,在不设置依赖的情况下,任务是并发、异步执行的
 17  
 18  而设置依赖的情况下,设置了依赖的任务按照所设定的顺序调用,而没有设置的,则是并发、异步执行
 19  */
 20 - (void)viewDidLoad {
 21     [super viewDidLoad];
 22     
 23     [self opeationListen];
 24 }
 25 
 26 - (void)invocationOperation
 27 {
 28     NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downLoadImage) object:nil];
 29     [invocation start];
 30 }
 31 
 32 - (void)blockOperation
 33 {
 34     NSBlockOperation *blockOperation = [[NSBlockOperation alloc] init];
 35     
 36     [blockOperation addExecutionBlock:^{
 37         for (int i = 0; i<10; i++) {
 38             NSLog(@"blockOperation------下载图片1---%@", [NSThread currentThread]);
 39             [NSThread sleepForTimeInterval:0.1];
 40         }
 41     }];
 42     
 43     [blockOperation addExecutionBlock:^{
 44         NSLog(@"blockOperation__2 --- %@",[NSThread currentThread]);
 45     }];
 46     
 47     [blockOperation addExecutionBlock:^{
 48         NSLog(@"blockOperation__3 --- %@",[NSThread currentThread]);
 49     }];
 50     
 51     [blockOperation start];
 52 }
 53 
 54 - (void)opeationListen
 55 {
 56     NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
 57         for (int i = 0; i<10; i++) {
 58             NSLog(@"opeationListen------下载图片---%@", [NSThread currentThread]);
 59         }
 60     }];
 61     
 62     blockOperation.completionBlock = ^{
 63         // ...下载完图片后想做事情
 64         NSLog(@"NSBlockOperation------下载图片完毕---%@", [NSThread currentThread]);
 65     };
 66     
 67     [blockOperation start];
 68 }
 69 
 70 - (void)downLoadImage
 71 {
 72     for (int i = 0; i < 10; i++) {
 73         NSLog(@"downLoadImage----正在下载----image---%@",[NSThread currentThread]);
 74         [NSThread sleepForTimeInterval:0.2];
 75     }
 76 }
 77 
 78 - (void)run
 79 {
 80     for (int i = 0; i<10; i++) {
 81         NSLog(@"run---正在跑步-----%@", [NSThread currentThread]);
 82         [NSThread sleepForTimeInterval:0.2];
 83     }
 84 }
 85 - (void)operationQueue
 86 {
 87     NSInvocationOperation *invocationOperation_1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downLoadImage) object:nil];
 88     
 89     NSInvocationOperation *invocationOperation_2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
 90     
 91     NSBlockOperation *blockOperation_1 = [NSBlockOperation blockOperationWithBlock:^{
 92         for (int i = 0; i < 10; i++) {
 93             NSLog(@"blockOperation_1------下载图片---%@", [NSThread currentThread]);
 94             [NSThread sleepForTimeInterval:0.1];
 95         }
 96     }];
 97     
 98     NSBlockOperation *blockOperation_2 = [NSBlockOperation blockOperationWithBlock:^{
 99         for (int i = 0; i < 10; i++) {
100             NSLog(@"blockOperation_2------下载视频---%@", [NSThread currentThread]);
101             [NSThread sleepForTimeInterval:0.1];
102         }
103     }];
104     
105     //设置依赖
106     [blockOperation_2 addDependency:blockOperation_1];
107     
108     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
109     
110     //设置最大并发数(一般设置为2~~3个)
111     queue.maxConcurrentOperationCount = 3;
112     
113     [queue addOperations:@[invocationOperation_1, invocationOperation_2, blockOperation_1, blockOperation_2] waitUntilFinished:NO];
114 }
115 @end

 

posted @ 2015-07-23 13:36  紫忆  阅读(344)  评论(0编辑  收藏  举报