美文网首页
NSOperationQueue使用

NSOperationQueue使用

作者: 唯风吹过六月 | 来源:发表于2020-06-09 13:09 被阅读0次

    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

    }

    相关文章

      网友评论

          本文标题:NSOperationQueue使用

          本文链接:https://www.haomeiwen.com/subject/rpkbzhtx.html