ios中的多線程的用法總結

來源:互聯網
上載者:User

標籤:線程和進程 nsoperationqu

ios中的多線程的用法總結

1、進程的基本概念

(1)每一個進程都是一個應用程式,都有獨立的記憶體空間,一般來說一個應用程式存在一個進程,但也有多個進程的情況

(2)同一個進程的線程共用記憶體中的記憶體和資源

2、多線程的基本概念

(1)每一個程式都有一個主線程,程式啟動時建立(調用main來啟動)。

(2)多線程技術表示,一個應用程式有多個線程,使用多線程能提供CPU的利用率,防止主線程被堵塞。

(3)任何有可能堵塞主線程的任務不要在主線程執行(如:訪問網路)。

(4)主線程的生命週期和應用程式綁定著,程式退出(結束)時,主線程也結束。

3、多線程的建立

                    /* 方法一*/   

    //第一種開啟新的線程調用 threadFunction

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadFunction) object:nil];

    [thread start];

                      /*方法二:*/

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

                       /*方法三:*/

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

 /*方法四:*/

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

    [operationQueue addOperationWithBlock:^(void){

        for (int i = 0; i<50; i++) {

            NSLog(@"多線程的運行");

        }

        

    }];

                   /*方法五:*/

    //建立一個線程隊列

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

    //設定線程執行的並發數

    operationQueue.maxConcurrentOperationCount = 1;

    //建立一個線程操作對象

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableThread1:) object:nil];

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(mutableThread2:) object:nil];

    //設定線程的優先順序

    operation2.queuePriority = NSOperationQueuePriorityHigh;

    //將線程添加到線程隊列中去

    [operationQueue addOperation:operation1];

    [operationQueue addOperation:operation2];

        }

    

   /*第六種方法*/

GCD

GCD是Grand Central Dispatch的縮寫,是一系列的BSD層面的介面,在Mac 10.6 和iOS4.0以後才引入的,且現在NSOperation和NSOperationQueue的多線程的實現就是基於GCD的。目前這個特性也被移植到 FreeBSD上了,可以查看libdispatch這個開源項目。


    //建立一個隊列

    dispatch_queue_t queue =dispatch_queue_create("test",NULL);

//建立非同步進程

    dispatch_async(queue, ^{

dispatch_sync(dispatch_get_main_queue(),^{

});

    });


  4、NSRunLoop的用法 

(1)Runloop是與線程有關的基礎架構的一部分,是用來規劃事件處理的,當有任務的時候Runloop會讓線程處理任務,當沒有任務的時候Runloop會讓線程處於休眠狀態。
(2)Runloop的管理不完全是自動的,我們必須在合適的時候開啟Runloop和處理到達的事件,Cocoa和Core Foundation都提供了Runloop對象來配置和管理線程的Runloop。我們的應用程式不需要顯示的建立這些對象,包括應用主線程在內的每一個線程都有一個相關的Runloop對象。而且只有第二線程是需要顯示地運行Runloop,主線程是不需要的,APP把主線程Runloop的配置和運行作為了應用程式啟動的一部分。

(3)NSRunLoop可以一直保持一個線程一直為活躍狀態,不會馬上銷毀掉。

(4)操作Runloop的兩個介面: 1.NSRunLoop Class Reference 2.CFRunLoop Reference

  5、定時器在多線程的使用

      在多線程中使用定時器必須開啟RunLoop,因為只有開啟RunLoop保持線程為活動狀態,才能保持定時器能不斷執行。  

    代碼:

  [selfperformSelectorInBackground:@selector(makeThread )withObject:nil];

- (void)mulitiThread{

    

    /*方法一:此方式建立的timer添加至NSRunLoop*/

      [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

      //開啟NSRunLoop來使線程保持存活狀態

     [[NSRunLoop currentRunLoop]run];

     */

    

    /*方法二:此方式建立的timer沒有添加至NSRunLoop*/

    NSTimer *timer = [NSTimertimerWithTimeInterval:1target:selfselector:@selector(timeAction)userInfo:nilrepeats:YES];

    [[NSRunLoopcurrentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

    [[NSRunLoopcurrentRunLoop]run];

    NSLog(@"線程結束");

}


- (void)timeAction{

    NSLog(@"timeAction");

}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.