ios多執行緒程式設計(NSThread)(NSOperation )(GCD)

theFeng-發表於2014-10-17

1:首先簡單介紹什麼叫執行緒

  • 可併發執行的,擁有最小系統資源,共享程式資源的基本排程單位。
  • 共用堆,自有棧(官方資料說明iOS主執行緒棧大小為1M,其它執行緒為512K)。
  • 併發執行進度不可控,對非原子操作易造成狀態不一致,加鎖控制又有死鎖的風險。

2:IOS中的執行緒

  • iOS主執行緒(UI執行緒),我們的大部分業務邏輯程式碼執行於主執行緒中。
  • 沒有特殊需求,不應引入執行緒增加程式複雜度。
  • 應用場景:邏輯執行時間過長,嚴重影響互動體驗(介面卡死)等。
  • IOS 多執行緒 有三種主要方法
    1)NSThread
    (2)NSOperation
    (3)GCD

    下面簡單介紹這三個方法 

    1.NSThread

        呼叫方法如下: 

        如函式需要輸入引數,可從object傳進去。
        (1) [NSThread detachNewThreadSelector:@selector(threadInMainMethod:) toTarget:self withObject:nil];
        (2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];
     [myThread start]; 
        (3) [obj performSelectorInBackground:@selector(threadMe) withObject:nil];

    提個問題:如果某個ViewController裡執行了一個Thread,Thread還沒結束的時候,這個ViewController被Release了,結果會如何? 

    經過的的測試,Thread不結束,ViewController一直保留,不會執行dealloc方法。

    2.NSOperation  

    NSoperation也是多執行緒的一種,NSopertaion有2種形式
      (1) 併發執行
           併發執行你需要過載如下4個方法
         //執行任務主函式,執行緒執行的入口函式
        - (void)start 
           //是否允許併發,返回YES,允許併發,返回NO不允許。預設返回NO
        -(BOOL)isConcurrent 
        - (BOOL)isExecuting
         //是否已經完成,這個必須要過載,不然放在放在NSOperationQueue裡的NSOpertaion不能正常釋放。
       - (BOOL)isFinished
       
       比如TestNSOperation:NSoperaion 過載上述的4個方法,
       宣告一個NSOperationQueue, NSOperationQueue *queue = [[[NSOperationQueue alloc ] init] autorelease];
      [queue addOperation:testNSoperation];

      它會自動呼叫TestNSOperation裡的 start函式,如果需要多個NSOperation,你需要設定queue的一些屬性,如果多個NSOperation之間又依賴關係,也可以設定,具體可以參考API 文件。 

    (2)非併發執行
      -(void)main
       只需要過載這個main方法就可以了。  

      3.GCD

      GCD很強大,我的使用經驗很少。但是scorpiozj 總結的比較全面(http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html

      同時,這篇文章也介紹的比較詳細 http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html 

         官方教程

    GCD是和block緊密相連的,所以最好先了解下block(可以檢視這裡).GCD是C level的函式,這意味著它也提供了C的函式指標作為引數,方便了C程式設計師.

    下面首先來看GCD的使用:

    dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

    async表明非同步執行,block代表的是你要做的事情,queue則是你把任務交給誰來處理了.(除了async,還有sync,delay,本文以async為例).

    之所以程式中會用到多執行緒是因為程式往往會需要讀取資料,然後更新UI.為了良好的使用者體驗,讀取資料的操作會傾向於在後臺執行,這樣以避免阻塞主執行緒.GCD裡就有三種queue來處理.

    1. Main queue:

      顧名思義,執行在主執行緒,由dispatch_get_main_queue獲得.和ui相關的就要使用Main Queue.

    2.Serial quque(private dispatch queue)

      每次執行一個任務,可以新增多個,執行次序FIFO(佇列,先進先出first input first out). 通常是指程式設計師生成的,比如:

    dispatch_queue_t myCustomQueue = dispatch_queue_create("example.MyCustomQueue", NULL);
    dispatch_async(myCustomQueue, ^{
        for (int abc=0;abc<100;abc++)
            {
                printf("1.Do some work here.\n");
            }
        });
    dispatch_async(myCustomQueue, ^{
        for (int abc=0;abc<100;abc++)
            {
                printf("2.Do some work here.\n");    
            }
        });
    dispatch_queue_t myCustomQueue2 = dispatch_queue_create("example.MyCustomQueue2", NULL);
    dispatch_async(myCustomQueue2, ^{
        for (int abc=0;abc<100;abc++)
            {
                printf("1. myCustomQueue2 Do some work here.\n");
            }
        });
    dispatch_async(myCustomQueue2, ^{
        for (int abc=0;abc<100;abc++)
            {
                printf("2. myCustomQueue2 Do some work here.\n");
            }
        });
    列印的結果必定會是 :然而,因為myCustomQueue 和 myCustomQueue2 是在兩個佇列中,所以在佇列myCustomQueue中:
    “1.Do some work here.” 在 “2.Do some work here.” 之前,而在myCustomQueue2佇列中:“1. myCustomQueue2 
    Do some work here.”在“2. myCustomQueue2 Do some work here.”之前。而在myCustomQueue和myCustomQueue2
    之中的任務是沒有先後的。及不是併發的。
    

    3. Concurrent queue(global dispatch queue):

    可以同時執行多個任務,每個任務的啟動時間是按照加入queue的順序,結束的順序依賴各自的任務.使用dispatch_get_global_queue獲得.

    所以我們可以大致瞭解使用GCD的框架:

    dispatch_queue_t newThread = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(newThread,^{[self downloadImage:ImageURL]; 
    }); 小節:NSThread的方式或許能做更快的切換,因為ARMv6或更高版本的處理器都提供了非常強大的執行緒切換機制。但是NSThread不會 採取多核的分派,因為這個系統介面首先要保證的是使用者執行緒的可靠性。 而Grand Central Dispatch顯式地利用分派佇列來做多核 分派排程,因此如果是在多核處理器上的話用G_C_D更快。如果你的處理器是單核心的話,那麼可以使用切換更快的NSThread。

相關文章