iOS 【Multithreading-NSBlockOperation简介(掌握)】

//
//  ViewController.m
//  45-NSOperation(NSBlockOperation)
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    test1 所需代码
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self test1];
//    });
    
    
    // 创建操作对象并将任务添加到其中
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1-----1.1-----%@",[NSThread currentThread]);
    }];
    
    // 可以为操作对象再添加一个任务
    [operation1 addExecutionBlock:^{
            NSLog(@"下载图片1-----1.2-----%@",[NSThread currentThread]);
    }];
    
    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片1-----1.3-----%@",[NSThread currentThread]);
    }];
    
    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片1-----1.4-----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片2-----2-----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片3-----3-----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片4-----4-----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *operation5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片5-----5-----%@",[NSThread currentThread]);
    }];
    
    
    
    NSBlockOperation *operation6 = [[NSBlockOperation alloc] init];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.1----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.2----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.3----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.4----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.5----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.6----%@",[NSThread currentThread]);
    }];
    
    [operation6 addExecutionBlock:^{
        NSLog(@"6.7----%@",[NSThread currentThread]);
    }];
    
    // 创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
//     将操作添加到队列中(此时自动异步执行,不需要写执行语句了就)
//        [queue addOperation:operation1];
//        [queue addOperation:operation2];
//        [queue addOperation:operation3];
//        [queue addOperation:operation4];
//        [queue addOperation:operation5];
   
    
    // 创建主队列
    NSOperationQueue *queue1 = [NSOperationQueue mainQueue];
    [queue1 addOperationWithBlock:^{
        NSLog(@"123");
    }];
    
    // 将操作添加到主队列(注意:如果操作中添加了很多任务,那么第一个添加上去的任务是在主线程执行的,后面添加上去的任务不一定在哪里执行,也是异步执行)
    [queue addOperation:operation6];
    
//    总结一点,添加到NSOperationQueue队列中的操作,都是会异步执行的。

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)test1 {
    
//    // 1、创建操作对象
//    NSBlockOperation *operation = [[NSBlockOperation alloc] init];
//    // 2、为操作对象添加任务
//    [operation addExecutionBlock:^{
//        NSLog(@"123123123");
//    }];
    
    // 创建操作对象并将任务添加到其中(此条语句等同于上面两句)
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1-----1.1-----%@",[NSThread currentThread]);
    }];
    
    // 可以为操作对象再添加一个任务
    [operation1 addExecutionBlock:^{
            NSLog(@"下载图片1-----1.2-----%@",[NSThread currentThread]);
    }];
    
    // 在用start方法进行执行的时候,任务数量大于1时才会异步执行。任务数量等于1时在当前线程中执行(注意是 当前线程 而不一定是主线程)
    [operation1 start];

}

@end

你可能感兴趣的:(ios,multithreading)