iOS- NSThread/NSOperation/GCD 三種多線程技術的對比及實現

來源:互聯網
上載者:User

 

 

每個NSThread對象對應一個線程,量級較輕(真正的多線程)NSOperation/NSOperationQueue 物件導向的線程技術(派發) 是基於C語言的架構,可以充分利用多核,是蘋果推薦使用的多線程技術

,在項目中很多架構技術分別使用了不同多線程技術。

 

三種多線程技術的對比                          
。線程同步對資料的加鎖會有一定的系統開銷  。iOS4.0+才能使用,是替代NSThread, NSOperation的高效和強大的技術

    [NSThread detachNewThreadSelector:@selector(bigDemo) toTarget:self withObject:nil];

 

 

 

       NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(bigDemo)     [thread start];

 

 

 

 

         [self performSelectorInBackground:@selector(bigDemo) withObject:nil];

 

                       }

 

 

 

          _queue = [[NSOperationQueue alloc] init];

 

 

     NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(opAction)                           [_queue addOperation:op1];

 

  mark 模仿下載網狀圖像 - (IBAction)operationDemo3:(           NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^         NSLog(           NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^         NSLog(           NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^         NSLog(                                 }

 

 

     [_queue addOperation:op2];

 

 

    ^

 

          [_queue setMaxConcurrentOperationCount:];

 

                      [op1 addDependency:op3];

 

 

 

 

  全域

  隊列

  非同步

    同步

  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );

    dispatch_queue_t queue = dispatch_queue_create(, DISPATCH_QUEUE_SERIAL);

 

 

 

 dispatch_async(dispatch_get_main_queue(), ^         NSLog(     });

 

 

 

 

 

                                                                 清澈Saup

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.