ios中的多線程的用法總結

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保持線程爲活動狀態,才能保持定時器能不斷執行。  

    代碼:

  [self performSelectorInBackground:@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 = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

    [[NSRunLoop currentRunLoop]run];

    NSLog(@"線程結束");

}


- (void)timeAction{

    NSLog(@"timeAction");

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章