多線程-NSBlockOperation

NSBlockOperation的使用與NSInvocationOperation時(shí)分類似,但也有不同之處

示例代碼:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // [self test1];
    // [self test2];
    [self test3];
}

// 開辟新線程
- (void)test1{
    
    // 1.創(chuàng)建NSBlockOperation操作對(duì)象
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%s-->%@",__func__,[NSThread currentThread]);
    }];
    
    // 2.創(chuàng)建隊(duì)列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 3.將操作添加到隊(duì)列中
    [queue addOperation:operation];
    
    // LOG: -[ViewController test1]_block_invoke--><NSThread: 0x7fa7c0d3a840>{number = 2, name = (null)}
}

// 不會(huì)開辟新線程
- (void)test2{
    
    // 1. 創(chuàng)建NSBlockOperation操作對(duì)象
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%s-->%@",__func__,[NSThread currentThread]);
    }];
    
    // 2. 開始執(zhí)行操作
    [operation start];
    
    // LOG: -[ViewController test2]_block_invoke--><NSThread: 0x7ff50b604f70>{number = 1, name = main}
    
}

// 多個(gè)操作調(diào)用start方法
- (void)test3{
    
    // 如果操作數(shù)大于1,那么大于1的那個(gè)操作會(huì)開辟一條新的線程成來(lái)執(zhí)行(大于1的多個(gè)操作將會(huì)開辟幾條線程,將由系統(tǒng)決定)
    
    // 1.創(chuàng)建NSBlockOperation操作對(duì)象
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%s-->%@",__func__,[NSThread currentThread]);
    }];
    
    // 2.給操作對(duì)象添加額外的操作
    [operation addExecutionBlock:^{
        NSLog(@"額外操作-->%@",[NSThread currentThread]);
    }];
    
    // 3. 調(diào)用start方法
    [operation start];
    
    /*      LOG:
     
     -[ViewController test3]_block_invoke--><NSThread: 0x7f88527019b0>{number = 1, name = main}

     額外操作--><NSThread: 0x7f885270d590>{number = 2, name = (null)}

     */
}
@end
  • NSBlockOperation與NSInvocationOperation的不同之處:

在test3方法中,實(shí)例化了NSBlockOperation對(duì)象后,并未添加到隊(duì)列中,而是先給操作對(duì)象添加了額外的操作,直接調(diào)用了start方法

實(shí)例化NSBlockOperation對(duì)象時(shí)封裝的操作仍然在當(dāng)前的線程執(zhí)行,并未開辟新線程,而額外操作會(huì)開辟新的線程去執(zhí)行
如果添加了多個(gè)額外操作,大于1的操作將會(huì)在開辟新線程執(zhí)行,至于開辟幾條線程,將由系統(tǒng)決定

其他寫法:

- (void)test4{
    
    // 1.創(chuàng)建隊(duì)列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 2.添加操作
    [queue addOperationWithBlock:^{
        NSLog(@"%s-->%@",__func__,[NSThread currentThread]);
    }];
    
    // LOG: -[ViewController test4]_block_invoke--><NSThread: 0x7fc6fa700810>{number = 2, name = (null)}
}

之前的寫法都是先實(shí)例化操作對(duì)象和隊(duì)列對(duì)象,通過(guò)隊(duì)列 "addOperation:" ,而這里我們只實(shí)例化了隊(duì)列,通過(guò)隊(duì)列對(duì)象 "addOperationWithBlock:" 方法,并且同樣開辟了新線程執(zhí)行

[queue addOperationWithBlock:^{
    NSLog(@"%s-->%@",__func__,[NSThread currentThread]);
}];

的內(nèi)部實(shí)現(xiàn)過(guò)程:
先將Block中的操作封裝成操作對(duì)象,然后將封裝后的操作對(duì)象添加到隊(duì)列中

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容