NSOperationQueue使用

1.NSInvocationOperation使用

-(NSOperationQueue*)operation_q{

    if (!_operation_q) {

        _operation_q=[[NSOperationQueue alloc]init];

    }

 return _operation_q;

}

[self WJ_NSInvocationOperation_demo];

-(void)WJ_NSInvocationOperation_demo{

    /**      - (void)start;

      - (void)main;

      @property (readonly, getter=isCancelled) BOOL cancelled;

      - (void)cancel;

      @property (readonly, getter=isExecuting) BOOL executing;

      @property (readonly, getter=isFinished) BOOL finished;

      @property (readonly, getter=isConcurrent) BOOL concurrent; // To be deprecated; use and override 'asynchronous' below

      @property (readonly, getter=isAsynchronous) BOOL asynchronous API_AVAILABLE(macos(10.8), ios(7.0), watchos(2.0), tvos(9.0));

      @property (readonly, getter=isReady) BOOL ready;

      - (void)addDependency:(NSOperation *)op;

      - (void)removeDependency:(NSOperation *)op;


      */


    _invocation_o=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocationTaget:)object:@"invocationTage"];

    [self.operation_q addOperation:_invocation_o];

    for(inti =0; i<10; i++) { 

        NSInvocationOperation*  invocation_O =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invocationTaget:)object:@(i)];


          //异步执行子线程执行充分利用cpu多核

            [self.operation_q addOperation:invocation_O];


          /***


           2020-06-03 13:36:44.740107+0800 IOStestOC[9218:184616] {number = 3, name = (null)}-----invocationTage

           2020-06-03 13:36:44.740149+0800 IOStestOC[9218:184603] {number = 5, name = (null)}-----1

           2020-06-03 13:36:44.740162+0800 IOStestOC[9218:184609] {number = 6, name = (null)}-----0

           2020-06-03 13:36:44.740184+0800 IOStestOC[9218:184608] {number = 4, name = (null)}-----2

           2020-06-03 13:36:44.740409+0800 IOStestOC[9218:184616] {number = 3, name = (null)}-----3

           2020-06-03 13:36:44.740453+0800 IOStestOC[9218:184603] {number = 5, name = (null)}-----4

           2020-06-03 13:36:44.740979+0800 IOStestOC[9218:184608] {number = 4, name = (null)}-----7

           2020-06-03 13:36:44.741052+0800 IOStestOC[9218:184616] {number = 3, name = (null)}-----8

           2020-06-03 13:36:44.741080+0800 IOStestOC[9218:184611] {number = 7, name = (null)}-----5

           2020-06-03 13:36:44.741101+0800 IOStestOC[9218:184603] {number = 5, name = (null)}-----9

           2020-06-03 13:36:44.741109+0800 IOStestOC[9218:184609] {number = 6, name = (null)}-----6

           */


      }



}

-(void)invocationTaget:(id)object{          NSLog(@"%@-----%@",[NSThread currentThread],object);    }

NSBlockOperation

-(void)WJ_NSBlockOperation_demo{

    for(inti =0; i<100; i++) {

        NSOperationQueue* part_operation =[[NSOperationQueue alloc]init];

           NSBlockOperation* block_operation =[NSBlockOperation blockOperationWithBlock:^{

                NSLog(@"%@---%d",[NSThread currentThread],i);

           }];

        /*** 添加到子线程执行执行 */


        //和 self.operation_q对比没有任何区别

           [part_operation addOperation:block_operation];

    }

    /****


     2020-06-03 13:54:20.488265+0800 IOStestOC[9672:200208] {number = 3, name = (null)}

     2020-06-03 13:54:20.488365+0800 IOStestOC[9672:200320] {number = 8, name = (null)}

     2020-06-03 13:54:20.488432+0800 IOStestOC[9672:200318] {number = 7, name = (null)}

     2020-06-03 13:54:20.488706+0800 IOStestOC[9672:200319] {number = 9, name = (null)}

     2020-06-03 13:54:20.488725+0800 IOStestOC[9672:200208] {number = 3, name = (null)}

     2020-06-03 13:54:20.488885+0800 IOStestOC[9672:200321] {number = 10, name = (null)}

     2020-06-03 13:54:20.488985+0800 IOStestOC[9672:200320] {number = 8, name = (null)}

     2020-06-03 13:54:20.489440+0800 IOStestOC[9672:200318] {number = 7, name = (null)}

     2020-06-03 13:54:20.489677+0800 IOStestOC[9672:200208] {number = 3, name = (null)}

     2020-06-03 13:54:20.489815+0800 IOStestOC[9672:200322] {number = 11, name = (null)}


     */

    /***

     可以直接在主线程执行

        [block_operation start];

        [block_operation main];

     */


}

NSOperationQueue_OperationWithBlock

-(void)NSOperationQueue_OperationWithBlock_demo{     //开发中使用比较方便子线程异步多线程安全执行不会出现死锁现象      for (int i =0; i<10; i++) {     [self.operation_q addOperationWithBlock:^{                 NSLog(@"%@---%d",[NSThread currentThread],i);         /**                    2020-06-03 14:08:34.133443+0800 IOStestOC[9976:211832] <NSThread: 0x600000922e40>{number = 7, name = (null)}---0          2020-06-03 14:08:34.133471+0800 IOStestOC[9976:211937] <NSThread: 0x6000009396c0>{number = 8, name = (null)}---2          2020-06-03 14:08:34.133487+0800 IOStestOC[9976:211836] <NSThread: 0x600000904080>{number = 3, name = (null)}---1          2020-06-03 14:08:34.133518+0800 IOStestOC[9976:211938] <NSThread: 0x60000093ee80>{number = 9, name = (null)}---3          2020-06-03 14:08:34.133662+0800 IOStestOC[9976:211937] <NSThread: 0x6000009396c0>{number = 8, name = (null)}---5          2020-06-03 14:08:34.133710+0800 IOStestOC[9976:211836] <NSThread: 0x600000904080>{number = 3, name = (null)}---4          2020-06-03 14:08:34.133724+0800 IOStestOC[9976:211832] <NSThread: 0x600000922e40>{number = 7, name = (null)}---6          2020-06-03 14:08:34.133785+0800 IOStestOC[9976:211938] <NSThread: 0x60000093ee80>{number = 9, name = (null)}---7          2020-06-03 14:08:34.133861+0800 IOStestOC[9976:211836] <NSThread: 0x600000904080>{number = 3, name = (null)}---8          2020-06-03 14:08:34.133892+0800 IOStestOC[9976:211937] <NSThread: 0x6000009396c0>{number = 8, name = (null)}---9                    */     }];      } }

线程间通信 

-(void)WJ_Inter_thread_communication{     //线程间通讯  

  [self.operation_q addOperationWithBlock:^{        //子线程处理数据        NSLog(@"%@",[NSThread currentThread]);      

  //主线程刷新数据      

  [[NSOperationQueue mainQueue] addOperationWithBlock:^{             NSLog(@"%@",[NSThread currentThread]);         }];   

    /**         2020-06-03 14:15:36.548571+0800 IOStestOC[10098:217774] <NSThread: 0x6000024f41c0>{number = 5, name = (null)}         2020-06-03 14:15:36.557632+0800 IOStestOC[10098:217501] <NSThread: 0x6000024b0380>{number = 1, name = main}         */     }];      }

 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //    NSLog(@"取消所有操作"); //    [self.operation_q cancelAllOperations]; //    NSLog(@"取消的操作数 %tu",self.operation_q.operationCount); //         //判断队列是否挂起!         if (self.operation_q.isSuspended) {            NSLog(@"继续 %tu",self.operation_q.operationCount);             self.operation_q.suspended = NO;                          NSLog(@"继续的操作数 %tu",self.operation_q.operationCount);         }else{             NSLog(@"暂停 %tu",self.operation_q.operationCount);             self.operation_q.suspended = YES;         }          }

设置同时进行最大并发数默认在子线程工作

-(void)WJ_maximum_number_concurrent{     //设置同时进行最大并发数     self.operation_q.maxConcurrentOperationCount = 3;      //添加操作进队列         for (int i=0; i< 30; i++) {             [self.operation_q addOperationWithBlock:^{                 [NSThread sleepForTimeInterval:3.0];                 NSLog(@"%@-----%d",[NSThread currentThread],i);             }];         }    /**     2020-06-03 14:28:52.510663+0800 IOStestOC[10446:228994] <NSThread: 0x60000111a480>{number = 5, name = (null)}-----1     2020-06-03 14:28:52.510663+0800 IOStestOC[10446:228998] <NSThread: 0x6000011104c0>{number = 6, name = (null)}-----0     2020-06-03 14:28:52.510663+0800 IOStestOC[10446:228993] <NSThread: 0x600001120040>{number = 4, name = (null)}-----2     2020-06-03 14:28:53.512770+0800 IOStestOC[10446:228993] <NSThread: 0x600001120040>{number = 4, name = (null)}-----5     2020-06-03 14:28:53.512772+0800 IOStestOC[10446:228994] <NSThread: 0x60000111a480>{number = 5, name = (null)}-----3     2020-06-03 14:28:53.512772+0800 IOStestOC[10446:228999] <NSThread: 0x600001120080>{number = 3, name = (null)}-----4     2020-06-03 14:28:54.517395+0800 IOStestOC[10446:228993] <NSThread: 0x600001120040>{number = 4, name = (null)}-----8     2020-06-03 14:28:54.517396+0800 IOStestOC[10446:228998] <NSThread: 0x6000011104c0>{number = 6, name = (null)}-----6     2020-06-03 14:28:54.517396+0800 IOStestOC[10446:228994] <NSThread: 0x60000111a480>{number = 5, name = (null)}-----7     2020-06-03 14:28:55.522114+0800 IOStestOC[10446:228993] <NSThread: 0x600001120040>{number = 4, name = (null)}-----9     2020-06-03 14:28:55.522114+0800 IOStestOC[10446:228999] <NSThread: 0x600001120080>{number = 3, name = (null)}-----10     */      }

添加依赖关系


-(void)WJ_Add_dependency{

    /**

    1,2,3完成,后续操作

     */

        //1.下载

        NSBlockOperation * op1 =[NSBlockOperation blockOperationWithBlock:^{

            [NSThread sleepForTimeInterval:0.5];

            NSLog(@"下载1---%@",[NSThread currentThread]);

        }];

        //2.下载

        NSBlockOperation * op2 =[NSBlockOperation blockOperationWithBlock:^{

              [NSThread sleepForTimeInterval:1.0];

            NSLog(@"下载2---%@",[NSThread currentThread]);

        }];

        //3.通知完成

        NSBlockOperation * op3 =[NSBlockOperation blockOperationWithBlock:^{

            NSLog(@"通知完成3---%@",[NSThread currentThread]);

        }];

        //NSOperation 提供了依赖关系

        [op2addDependency:op1];

        [op3addDependency:op2];


    //添加到队列中  waitUntilFinished:是否等待


    /**

     //YES会主当前线程等待打印结果

    2020-06-03 15:05:45.214593+0800 IOStestOC[11433:261296] 下载1---{number = 6, name = (null)}

    2020-06-03 15:05:46.216319+0800 IOStestOC[11433:261295] 下载2---{number = 7, name = (null)}

    2020-06-03 15:05:46.216840+0800 IOStestOC[11433:261077] 什么时候执行打印{number = 1, name = main}

    2020-06-03 15:05:46.231206+0800 IOStestOC[11433:261077] 通知完成3---{number = 1, name = main}

    */


    /**

      //NO 不会卡线程 打印结果

     2020-06-03 15:08:03.482995+0800 IOStestOC[11508:263228] 什么时候执行打印{number = 1, name = main}

     2020-06-03 15:08:03.987297+0800 IOStestOC[11508:263486] 下载1---{number = 3, name = (null)}

     2020-06-03 15:08:04.990830+0800 IOStestOC[11508:263486] 下载2---{number = 3, name = (null)}

     2020-06-03 15:08:04.991214+0800 IOStestOC[11508:263228] 通知完成3---{number = 1, name = main}

     */

//        //先异步执行代码

//        [self.operation_q addOperations:@[op1,op2] waitUntilFinished:NO];

//        //主线程刷新界面 或者必须在主线程的操作

//        [[NSOperationQueue mainQueue] addOperation:op3];

//

//

//      NSLog(@"什么时候执行打印%@",[NSThread currentThread]);

    //全部在子线程的操作代码

    /***

     //waitUntilFinished:YES

     [self.operation_q addOperations:@[op1,op2,op3] waitUntilFinished:YES];

             NSLog(@"什么时候执行打印%@",[NSThread currentThread]);

     2020-06-03 15:10:23.991561+0800 IOStestOC[11591:265688] 下载1---{number = 3, name = (null)}

     2020-06-03 15:10:24.993174+0800 IOStestOC[11591:265944] 下载2---{number = 8, name = (null)}

     2020-06-03 15:10:24.993525+0800 IOStestOC[11591:265688] 通知完成3---{number = 3, name = (null)}

     2020-06-03 15:10:24.993850+0800 IOStestOC[11591:265435] 什么时候执行打印{number = 1, name = main}

     */


    /**

     //waitUntilFinished:NO

     [self.operation_q addOperations:@[op1,op2,op3] waitUntilFinished:NO];

             NSLog(@"什么时候执行打印%@",[NSThread currentThread]);

     2020-06-03 15:02:16.051287+0800 IOStestOC[11285:257420] 什么时候执行打印{number = 1, name = main}

     2020-06-03 15:02:16.553478+0800 IOStestOC[11285:257662] 下载1---{number = 3, name = (null)}

     2020-06-03 15:02:17.555200+0800 IOStestOC[11285:257666] 下载2---{number = 7, name = (null)}

     2020-06-03 15:02:17.555561+0800 IOStestOC[11285:257662] 通知完成3---{number = 3, name = (null)}


     */

   // maxConcurrentOperationCount

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335