iOS之多線程分析

對于多線程的理解:
多線程: 利用多核處理器的優勢,實現多個操作的并發執行,使系統資源得到充分利用,提高運行效率,減少主線程的工作量,將非UI層面的耗時操作放在其他線程中執行,使得UI流暢,交互響應迅速。
幾個概念:
進程:
1、系統中運行的一個應用程序
2、進程是系統資源分配和調度的基本單位
3、系統中或許有多個進程,進程間相互獨立,有各自的空間,但是共享系統的資源
線程:
1、一個進程至少有一個線程,稱為主線程
2、進程中的所有任務都是在線程中執行的
3、進程中的多個線程共享進程中的資源
多線程:
一個進程中開啟多條線程,每個線程負責執行一個任務,這樣就提高了運行效率
多線程原理:
1、同一時間,CPU只能處理一條線程,只能有一個線程工作
2、多線程的并發執行,實際上是CPU在多條線程之間來回切換
3、CPU快速的來回切換,就造成了很多線程并發執行的假象
任務:
1、任務分為同步任務 和 異步任務
2、同步任務不會開啟新的線程,而是在當前線程執行
3、異步任務可能會開啟新的線程(當前沒有空閑線程的情況下)
隊列:
1、任務在進入線程執行之前存放在隊列里等待
2、隊列分為串行隊列 和 并行隊列
3、串行隊列中的任務,不管同步異步,都是串行執行(注意:主隊列不管是同步還是異步任務,都不會開啟新線程,創建的串行隊列,添加異步任務會創建新線程,但是結果都是串行執行)
4、并行隊列中的任務,根據同步異步的不同,決定誰先執行,誰繼續等待
5、并行隊列同步任務,會等待主線程空閑后執行
6、并行隊列異步任務,在主線程不阻塞的情況下,在沒有空閑線程的情況下,會創建新的線程執行,有空閑線程的情況下就在此空閑線程中執行
7、一個比較特殊的隊列:主隊列,也是串行隊列,主隊列就一個可執行任務的線程就是主線程,UI的操作必須放在主線程()中進行,主線程控制著整個應用程序的運行

代碼驗證:以GCD為例

上半部分 :串行隊列

在主隊列(串行隊列)中添加同步任務

dispatch_sync(dispatch_get_main_queue(), ^{
    for (int i=0; i<5; i++) {
          NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
 });

結論:系統崩潰,因為在主隊列開啟同步任務時,主隊列是串行隊列,里面的線程是有順序的,先執行完一個線程才執行下一個線程。而主隊列始終就只有一個主線程,主線程還是無限循環的,是不會執行完畢的,除非關閉應用程序。
因此在主線程開啟一個同步任務,同步任務會想搶占執行的資源,而主線程任務一直在執行某些操作,不肯放手。兩個的優先級都很高,最終導致死鎖,阻塞線程了。
在主隊列(串行隊列)中添加異步任務

    for (int i=-5; i<0; i++) {  //主隊列  主線程阻塞
        NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
  
    //添加一個異步任務 , 開啟線程
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i=5; i<10; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i=10; i<15; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
//打印結果:
<NSThread: 0x60000006d480>{number = 1, name = main},====-5
<NSThread: 0x60000006d480>{number = 1, name = main},====-4
<NSThread: 0x60000006d480>{number = 1, name = main},====-3
<NSThread: 0x60000006d480>{number = 1, name = main},====-2
<NSThread: 0x60000006d480>{number = 1, name = main},====-1
<NSThread: 0x60000006d480>{number = 1, name = main},====5
<NSThread: 0x60000006d480>{number = 1, name = main},====6
<NSThread: 0x60000006d480>{number = 1, name = main},====7
<NSThread: 0x60000006d480>{number = 1, name = main},====8
<NSThread: 0x60000006d480>{number = 1, name = main},====9
<NSThread: 0x60000006d480>{number = 1, name = main},====10
<NSThread: 0x60000006d480>{number = 1, name = main},====11
<NSThread: 0x60000006d480>{number = 1, name = main},====12
<NSThread: 0x60000006d480>{number = 1, name = main},====13
<NSThread: 0x60000006d480>{number = 1, name = main},====14

結論:1、串行隊列 順序執行 2、主隊列的串行隊列,即使添加異步任務也不會開啟子線程
在自定義的串行隊列中添加同步任務

   dispatch_queue_t serialQueue = dispatch_queue_create("chuanxingduilie",  
                                                                                  DISPATCH_QUEUE_SERIAL);
    for (int i=-5; i<0; i++) {  //主隊列 主線程阻塞
        NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
    // 串行隊列 
    dispatch_sync(serialQueue, ^{
        for (int i=0; i<5; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
打印結果:
<NSThread: 0x60000007afc0>{number = 1, name = main},====-5
<NSThread: 0x60000007afc0>{number = 1, name = main},====-4
<NSThread: 0x60000007afc0>{number = 1, name = main},====-3
<NSThread: 0x60000007afc0>{number = 1, name = main},====-2
<NSThread: 0x60000007afc0>{number = 1, name = main},====-1
<NSThread: 0x60000007afc0>{number = 1, name = main},====0
<NSThread: 0x60000007afc0>{number = 1, name = main},====1
<NSThread: 0x60000007afc0>{number = 1, name = main},====2
<NSThread: 0x60000007afc0>{number = 1, name = main},====3
<NSThread: 0x60000007afc0>{number = 1, name = main},====4

結論:只要是串行隊列中添加同步任務,都不會開啟子線程,都是在主線程中執行

在自定義的串行隊列中添加異步任務

dispatch_queue_t serialQueue = dispatch_queue_create("chuanxingduilie", DISPATCH_QUEUE_SERIAL);
    for (int i=-5; i<0; i++) { //主隊列 主線程阻塞
        NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
    //串行隊列  同步任務 主線程
    dispatch_sync(serialQueue, ^{
        for (int i=0; i<5; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
    //串行隊列非主隊列   異步任務 , 開啟線程
    dispatch_async(serialQueue, ^{
        for (int i=5; i<10; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    }); 
    //串行隊列非主隊列   異步任務 , 開啟線程
    dispatch_async(serialQueue, ^{
        for (int i=10; i<15; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
打印結果:
<NSThread: 0x60000007da80>{number = 1, name = main},====-5
<NSThread: 0x60000007da80>{number = 1, name = main},====-4
<NSThread: 0x60000007da80>{number = 1, name = main},====-3
<NSThread: 0x60000007da80>{number = 1, name = main},====-2
<NSThread: 0x60000007da80>{number = 1, name = main},====-1
<NSThread: 0x60000007da80>{number = 1, name = main},====0
<NSThread: 0x60000007da80>{number = 1, name = main},====1
<NSThread: 0x60000007da80>{number = 1, name = main},====2
<NSThread: 0x60000007da80>{number = 1, name = main},====3
<NSThread: 0x60000007da80>{number = 1, name = main},====4
<NSThread: 0x600000269a40>{number = 3, name = (null)},====5
<NSThread: 0x600000269a40>{number = 3, name = (null)},====6
<NSThread: 0x600000269a40>{number = 3, name = (null)},====7
<NSThread: 0x600000269a40>{number = 3, name = (null)},====8
<NSThread: 0x600000269a40>{number = 3, name = (null)},====9
<NSThread: 0x600000269a40>{number = 3, name = (null)},====10
<NSThread: 0x600000269a40>{number = 3, name = (null)},====11
<NSThread: 0x600000269a40>{number = 3, name = (null)},====12
<NSThread: 0x600000269a40>{number = 3, name = (null)},====13
<NSThread: 0x600000269a40>{number = 3, name = (null)},====14

結論:1、非主隊列串行隊列,添加異步任務會創建子線程 2、異步任務也可能不會開啟新的子線程,如果有空閑線程的情況下(如代碼中第二個異步任務)
3、串行隊列任務都是順序執行,不管是同步任務還是異步任務

下半部分:并行隊列

在GCD全局隊列global隊列(并行隊列)中開啟同步任務

    for (int i=-5; i<0; i++) { //主隊列 主線程阻塞
        NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
    //添加一個同步任務 , 不開啟線程,在主線程中執行,等待主線程空閑后 才執行此處
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        for (int i=5; i<10; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
打印結果:
<NSThread: 0x60000006a240>{number = 1, name = main},====-5
<NSThread: 0x60000006a240>{number = 1, name = main},====-4
<NSThread: 0x60000006a240>{number = 1, name = main},====-3
<NSThread: 0x60000006a240>{number = 1, name = main},====-2
<NSThread: 0x60000006a240>{number = 1, name = main},====-1
<NSThread: 0x60000006a240>{number = 1, name = main},====5
<NSThread: 0x60000006a240>{number = 1, name = main},====6
<NSThread: 0x60000006a240>{number = 1, name = main},====7
<NSThread: 0x60000006a240>{number = 1, name = main},====8
<NSThread: 0x60000006a240>{number = 1, name = main},====9

結論:在并行隊列中,添加同步任務不會開啟新的線程,仍是在主線程中執行

在GCD全局隊列global隊列中開啟異步任務

   for (int i=-5; i<0; i++) { //主隊列 主線程阻塞
        NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
    }
 //開啟子線程,與主線程并發執行
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=10; i<15; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
    //添加一個同步任務 , 不開啟線程
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        for (int i=5; i<10; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
        }
    });
打印結果:
<NSThread: 0x600000078ac0>{number = 1, name = main},====-5
<NSThread: 0x600000078ac0>{number = 1, name = main},====-4
<NSThread: 0x600000078ac0>{number = 1, name = main},====-3
<NSThread: 0x600000078ac0>{number = 1, name = main},====-2
<NSThread: 0x600000078ac0>{number = 1, name = main},====-1
<NSThread: 0x600000078ac0>{number = 1, name = main},====5
<NSThread: 0x600000262740>{number = 3, name = (null)},====10
<NSThread: 0x600000078ac0>{number = 1, name = main},====6
<NSThread: 0x600000262740>{number = 3, name = (null)},====11
<NSThread: 0x600000078ac0>{number = 1, name = main},====7
<NSThread: 0x600000262740>{number = 3, name = (null)},====12
<NSThread: 0x600000078ac0>{number = 1, name = main},====8
<NSThread: 0x600000078ac0>{number = 1, name = main},====9
<NSThread: 0x600000262740>{number = 3, name = (null)},====13
<NSThread: 0x600000262740>{number = 3, name = (null)},====14

結論: 1、 并行隊列,線程可以并發執行 2、并行隊列 添加同步任務,任務會排在主線程后面等待主線程空閑

總結:
1、主隊列是特殊的串行隊列,只有一個主線程,主線程在阻塞的前提下,后面所有的并發還是串行的操作都必須等待主線程空閑

2、不管是什么隊列,同步任務絕不會開啟新的線程,只能在主線程后排隊,優先級較高,由此看來,主線程中的任務不一定來自主隊列,但主隊列的任務一定在主線程執行

3、異步任務,優先級低,在線程中沒有執行順序,主要看CPU閑不閑,在主隊列中即使異步任務也不會開啟新線程,但是在其他隊列中會開啟新的線程

NSOperationQueue
NSOperationQueue同樣是多線程實現的主要方式,底層依舊是GCD,只不過本身更加面向對象
1、主隊列 [NSOperationQueue mainQueue];
2、自定義隊列 ,不設置maxConcurrentOperationCount 最大并發數的情況下,系統會根據CPU和資源情況決定隊列的最大并發數,如果設置maxConcurrentOperationCount=1,就說明此隊列是串行隊列,

    NSOperationQueue *serialQueue = [[NSOperationQueue alloc]init];
    serialQueue.maxConcurrentOperationCount = 1;
    
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testInvocation) object:nil];
    [serialQueue addOperation:operation];
    
    [serialQueue addOperationWithBlock:^{
        for (int i=5; i<10; i++) {
            NSLog(@"thread=====%@,====%d",[NSThread currentThread],i);
            
        }
    }];

3、另外NSOperationQueue的任務類:NSBlockOperation和NSInvocationOperation都是異步任務,沒有同步任務,當然,向串行隊列中添加異步任務,同樣可是達到串行的效果

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容