多线程-NSOperation
NSOperation
NSOperation的使用:
NSOperation需要和和NSOperationQueue實(shí)現(xiàn)多線程編程
實(shí)現(xiàn)的具體步驟:
注意:NSOperation是一個(gè)抽象類,不具備分裝操作的能力必須使用它的子類
- NSInvocationOperation
- NSBlockOperation
- 自定義子類繼承NSOperation,實(shí)現(xiàn)內(nèi)部相應(yīng)的方法(-main方法)
這里介紹一下上面提到的操作 想到于GCD中的隊(duì)列,封裝一些你要中的事情(任務(wù))
NSOperation各個(gè)子類的基本使用:
NSInvocationOperation
?
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {NSInvocationOperation *invovationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task) object:nil];[invovationOperation start]; }- (void)task {NSLog(@"%@", [NSThread currentThread]); }?
輸出結(jié)果:
2016-03-29 23:14:31.262 test1[1440:105145] <NSThread: 0x7fcba5101dc0>{number = 1, name = main}分析,說明使用NSInvocationOperation并不會(huì)開辟子線程執(zhí)行封裝的任務(wù),我們?cè)诳匆幌翹SBlockOperation
?
NSBlockOperation的使用
NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i<3; i++) {NSLog(@"A------%@", [NSThread currentThread]);};}];[blockOp start];輸出結(jié)果:
2016-03-29 23:21:00.211 test1[1470:107920] A------<NSThread: 0x7fda63805010>{number = 1, name = main} 2016-03-29 23:21:00.212 test1[1470:107920] A------<NSThread: 0x7fda63805010>{number = 1, name = main} 2016-03-29 23:21:00.212 test1[1470:107920] A------<NSThread: 0x7fda63805010>{number = 1, name = main}分析得到結(jié)果:NSBlockOperation單獨(dú)使用也需要調(diào)用start方法,并且不會(huì)開辟子線程執(zhí)行任務(wù),但是我們看一下下面的情況,當(dāng)block追加操作的時(shí)候,追加的操作會(huì)在子線程中執(zhí)行并且是并發(fā)執(zhí)行的
注意: 默認(rèn)情況下,調(diào)用了start方法后并不會(huì)開一條新線程去執(zhí)行操作,而是在當(dāng)前線程同步執(zhí)行操作 只有將NSOperation放到一個(gè)NSOperationQueue中,才會(huì)異步執(zhí)行操作 并且內(nèi)部會(huì)調(diào)用start方法 NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i<3; i++) {NSLog(@"A------%@", [NSThread currentThread]);};}]; /********************************blcokOperation追加任務(wù)********************************/[blockOp addExecutionBlock:^{for (int i = 0; i<3; i++) {NSLog(@"B------%@", [NSThread currentThread]);};}]; /********************************blcokOperation追加任務(wù)********************************/[blockOp addExecutionBlock:^{for (int i = 0; i<3; i++) {NSLog(@"C------%@", [NSThread currentThread]);};}];[blockOp start];輸出結(jié)果:
2016-03-29 23:24:58.883 test1[1508:110154] A------<NSThread: 0x7fd3905062c0>{number = 1, name = main} 2016-03-29 23:24:58.883 test1[1508:110187] C------<NSThread: 0x7fd390700130>{number = 2, name = (null)} 2016-03-29 23:24:58.883 test1[1508:110186] B------<NSThread: 0x7fd3919b7290>{number = 3, name = (null)} 2016-03-29 23:24:58.883 test1[1508:110154] A------<NSThread: 0x7fd3905062c0>{number = 1, name = main} 2016-03-29 23:24:58.883 test1[1508:110154] A------<NSThread: 0x7fd3905062c0>{number = 1, name = main} 2016-03-29 23:24:58.883 test1[1508:110186] B------<NSThread: 0x7fd3919b7290>{number = 3, name = (null)} 2016-03-29 23:24:58.883 test1[1508:110187] C------<NSThread: 0x7fd390700130>{number = 2, name = (null)} 2016-03-29 23:24:58.884 test1[1508:110186] B------<NSThread: 0x7fd3919b7290>{number = 3, name = (null)} 2016-03-29 23:24:58.884 test1[1508:110187] C------<NSThread: 0x7fd390700130>{number = 2, name = (null)}?
注意:只要NSBlockOperation封裝的操作數(shù) > 1,就會(huì)異步執(zhí)行操作
將操作添加進(jìn)NSOperationQueue的使用
首先我們要靠接NSOperationQueue的兩種類型及創(chuàng)建方式
NSOperationQueue *queue = [NSOperationQueue mainQueue];
?注意:通過mainQueue 創(chuàng)建的隊(duì)列中的任務(wù),不會(huì)創(chuàng)建子線程,在主隊(duì)列中執(zhí)行
NSOperationQueue *queue = [[NSOperationQueue alloc] init];?注意:通過alloc init的方式創(chuàng)建的隊(duì)列會(huì)開啟子線程執(zhí)行任務(wù),并且是并發(fā)執(zhí)行任務(wù)的,那么我們?cè)趺聪騁CD中那樣讓隊(duì)列中的任務(wù)串行執(zhí)行任務(wù)呢
那么可以以通過線面的屬性來設(shè)置最大的并發(fā)數(shù) 當(dāng)值為1的時(shí)候,隊(duì)列會(huì)開啟子線程并且是串行執(zhí)行隊(duì)列中的任務(wù),在實(shí)際的開發(fā),建議一般創(chuàng)建最多子線程的條數(shù)為3~5
queue.maxConcurrentOperationCount = 1;?
隊(duì)列的取消、暫停、恢復(fù)
?
操作依賴
?
操作的監(jiān)聽
?
自定義NSOperation
?
自定義NSOperation下載圖片思路 – 無沙盒緩存
?
轉(zhuǎn)載于:https://www.cnblogs.com/mshong/p/5335399.html
總結(jié)
以上是生活随笔為你收集整理的多线程-NSOperation的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Socket.io发送消息含义
- 下一篇: Hbase的基本认识