NSOperation常見用法

一提到多線程,腦海中大致會出現(xiàn)三種常用的方法NSThread,NSOperation,GCD。可是...你還在用GCD嗎?看看基于GCD封裝的NSOperation吧,更加的面相對象,方便閱讀。
然鵝...... NSOperation是抽象基類。
老爹難使喚,只能使喚他的倆兒子NSInvocationOperation和NSBlockOperation。來來來,上demo。

NSInvocationOperation
-(void)InvocationOperation
{
    NSInvocationOperation *invo = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testInvocation) object:nil];
    //記得手動start??!
    [invo start];
}

-(void)testInvocation
{
    NSLog(@"%@",[NSThread currentThread]);
}

運行結(jié)果:可以看到,默認(rèn)是在主線程的

19:05:38.346 [2689:142129] <NSThread: 0x600000071200>{number = 1, name = main}
NSBlockOperation

為了效果好一點,多來幾個block試試看

-(void)BlockOperation
{
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1---%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"2---%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"3---%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"4---%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"5---%@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"6---%@",[NSThread currentThread]);
    }];
    //你手動start了嘛?!
    [blockOperation start];
}

運行結(jié)果

19:00:35.899 [2656:140762] 4---<NSThread: 0x608000078000>{number = 5, name = (null)}
19:00:35.899 [2656:140730] 1---<NSThread: 0x60800006dc00>{number = 1, name = main}
19:00:35.899 [2656:140763] 3---<NSThread: 0x608000070380>{number = 4, name = (null)}
19:00:35.899 [2656:140765] 2---<NSThread: 0x60000007ca40>{number = 3, name = (null)}
19:00:35.899 [2656:140762] 5---<NSThread: 0x608000078000>{number = 5, name = (null)}
19:00:35.899 [2656:140730] 6---<NSThread: 0x60800006dc00>{number = 1, name = main}

總結(jié)一下:
1.NSBlockOperation確實實現(xiàn)了多線程。但是!并非是將所有的block都放到放到了子線程中,主線程和子線程都有啊。怎么回事?
辣么,再觀察線程id號:
[2656:140762]和[2656:140730]出現(xiàn)過兩次。它會優(yōu)先將block放到主線程中執(zhí)行,若主線程已有待執(zhí)行的代碼,就開辟新的線程。
2.相同blockOperation中的代碼是同步執(zhí)行的。這個可以在打印的運行時間看出來19:00:35.899。

NSInvocationOperation和NSBlockOperation挺厲害啊......于是NSOperationQueue表示不服。

NSOperationQueue
-(void)OpertationQueue
{
    //invocation添加到queue的寫法
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *invo = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testInvocation) object:nil];
    [queue addOperation:invo];
    
    //block添加到queue的方法
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"blockOperation---%@",[NSThread currentThread]);
    }];
    [queue addOperation:blockOperation];
    
    //更簡單的方法,直接添加
    [queue addOperationWithBlock:^{
        NSLog(@"addOperationWithBlock---%@",[NSThread currentThread]);
    }];
}

運行結(jié)果:可以看到三個線程異步執(zhí)行
1.直接add到NSOperationQueue就可以,不需要手動startNSOperationQueue會在合適的時機自動調(diào)用。
2.add到NSOperationQueue之后,會自動到子線程中。

20:20:25.095 [2971:164505] <NSThread: 0x60800007de00>{number = 3, name = (null)}
20:20:25.095 [2971:164519] blockOperation---<NSThread: 0x60800007de40>{number = 4, name = (null)}
20:20:25.095 [2971:164504] addOperationWithBlock---<NSThread: 0x600000079500>{number = 5, name = (null)}

......看黑板?。?br> 這個NSOperation還能添加依賴,控制線程的執(zhí)行順序,一句話的事兒

addDependency
-(void)addDependency{
    NSInvocationOperation *invo1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    NSInvocationOperation *invo2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [invo1 addDependency:invo2];
    [queue addOperation:invo1];
    [queue addOperation:invo2];
}

-(void)test1
{
    NSLog(@"invo1---%@",[NSThread currentThread]);
}

-(void)test2
{
    NSLog(@"invo2---%@",[NSThread currentThread]);
}

運行結(jié)果:設(shè)置了invo1依賴于invo2,運行幾遍都是這個順序了

20:40:14.201 [3207:175388] invo2---<NSThread: 0x60800006fe00>{number = 3, name = (null)}
20:40:14.202 [3207:175389] invo1---<NSThread: 0x608000070640>{number = 4, name = (null)}

??warning
1.不要循環(huán)依賴,不然會循環(huán)引用。
2.如果想混合使用invocaiton和block,多上點心。
3.依賴無國界,不同隊列的NSOperation對象設(shè)置的依賴關(guān)系。

優(yōu)先級和依賴關(guān)系的區(qū)別

看看Foundation---NSOperation.h你會發(fā)現(xiàn)可以直接設(shè)置優(yōu)先級。Bu t!優(yōu)先級和依賴關(guān)系是有區(qū)別的:優(yōu)先級不能替代依賴關(guān)系,優(yōu)先級只是對已經(jīng)準(zhǔn)備好的 operations確定執(zhí)行順序。先滿足依賴關(guān)系,然后再根據(jù)優(yōu)先級從所有準(zhǔn)備好的操作中選擇優(yōu)先級最高的那個執(zhí)行。

typedef NS_ENUM(NSInteger, NSOperationQueuePriority) {
    NSOperationQueuePriorityVeryLow = -8L,
    NSOperationQueuePriorityLow = -4L,
    NSOperationQueuePriorityNormal = 0,
    NSOperationQueuePriorityHigh = 4,
    NSOperationQueuePriorityVeryHigh = 8
};
其他可能用到的屬性

setMaxConcurrentOperationCount; //設(shè)置最大并發(fā)數(shù)
[queue setSuspended:YES]; //暫停隊列
[queue setSuspended:NO]; //繼續(xù)隊列
[operation waitUntilFinished]; //會阻塞當(dāng)前線程,等到某個operation執(zhí)行完畢
[queue waitUntilAllOperationsAreFinished]; //阻塞當(dāng)前線程,等待queue的所有操作執(zhí)行完畢
所以。。。有木有發(fā)現(xiàn)是基于GCD封裝的

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容