Objective-c併發編程 -- GCD代碼演示

併發隊列(全局隊列) + 同步任務

沒有開啓新的線程, 任務是逐個執行的併發

代碼

// 獲取全局隊列
    // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 建立併發隊列
    dispatch_queue_t queue = dispatch_queue_create("myQueur", DISPATCH_QUEUE_CONCURRENT);
    
    // 在隊列裏添加任務
    // 同步任務
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

輸出

GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}

併發隊列(全局隊列) + 異步任務

開啓了新線程, 任務是併發的異步

代碼

// 獲取全局隊列
    // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 建立併發隊列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_CONCURRENT);
    
    // 異步任務
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

輸出

GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}

串行隊列 + 同步任務

沒有開啓新的線程, 任務是逐個執行的async

代碼

// 建立併發隊列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    
    // 同步任務
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

輸出

GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}

串行隊列 + 異步任務

開啓新的線程, 任務是逐個執行的spa

代碼

// 建立串行隊列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    
    // 異步任務
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

輸出

GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}

主隊列 + 同步任務

會形成死鎖的線程【不能在主隊列裏添加同步任務】線程

代碼

// 得到主隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 同步任務
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });

主隊列 + 異步任務

沒有開啓新的線程, 任務是逐個執行的code

代碼

// 得到主隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 異步任務
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

輸出

GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
相關文章
相關標籤/搜索