iOS - GCD隊列、任務組合

1. 單個隊列

dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concur", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    NSLog(@"--主隊列 %@", [NSThread currentThread]);
    
    //1. 同步串行(不會開啓新線程)
    dispatch_sync(serialQueue, ^{
        NSLog(@"--同步串行 %@", [NSThread currentThread]);
    });
    
    //2. 同步並行(不會開啓新線程)
    dispatch_sync(concurrentQueue, ^{
        NSLog(@"--同步並行: %@", [NSThread currentThread]);
    });
    
    //3. 異步串行(會開啓新線程)
    dispatch_async(serialQueue, ^{
        NSLog(@"--異步串行: %@", [NSThread currentThread]);
    });

    //4. 異步並行(會開啓新線程)
    dispatch_async(concurrentQueue, ^{
        NSLog(@"--異步並行: %@", [NSThread currentThread]);
    });
    
    //5. 異步主隊列(沒有開啓新線程)
    dispatch_async(mainQueue, ^{
        NSLog(@"--異步主隊列: %@", [NSThread currentThread]);
    });
    
    //6. 主線程同步(死鎖)
    dispatch_sync(mainQueue, ^{
        NSLog(@"--主線程同步: %@", [NSThread  currentThread]);
    });
複製代碼

2. 隊列嵌套

NSLog(@"--主線程: %@", [NSThread currentThread]);
    dispatch_queue_t serialQueue = dispatch_queue_create("com.objcc.serial1", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue = dispatch_queue_create("com.objcc.concurrent1", DISPATCH_QUEUE_CONCURRENT);
    

    //1. 異步併發嵌套同一個同步併發(異步併發建立新線程,同步併發沒有建立新線程)
    dispatch_async(concurrentQueue, ^{
        NSLog(@"--異步併發: %@", [NSThread currentThread]);
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"--同步併發: %@", [NSThread currentThread]);
        });
    });

    //2. 異步併發嵌套同同一個異步併發隊列(外層建立新線程,內層不建立新線程)
    dispatch_async(concurrentQueue, ^{
        NSLog(@"--異步併發1: %@",[NSThread currentThread]);
        dispatch_async(concurrentQueue, ^{
            NSLog(@"--異步併發2: %@",[NSThread currentThread]);
        });
    });
    
    
    //3. 同步併發嵌套同一個同步併發隊列(都不會開啓新線程)
    dispatch_sync(concurrentQueue, ^{
        NSLog(@"--同步併發1: %@",[NSThread currentThread]);
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"--同步併發1: %@",[NSThread currentThread]);
        });
    });
     
    
    //3. 同步併發嵌套異步併發(異步併發會開啓新線程)
    dispatch_sync(concurrentQueue, ^{
        NSLog(@"--同步併發1: %@",[NSThread currentThread]);
        dispatch_async(concurrentQueue, ^{
            NSLog(@"--異步併發2: %@",[NSThread currentThread]);
        });
    });
     
    //4. 異步串行,嵌套同一個同步串行(外環開啓新線程,內環死鎖卡崩潰)
    dispatch_async(serialQueue, ^{
        NSLog(@"--異步串行1: %@",[NSThread currentThread]);
        dispatch_sync(serialQueue, ^{
            NSLog(@"--同步串發2: %@",[NSThread currentThread]);
        });
    });
     
    
    //6. 異步串行嵌套同一個異步串行(外環開啓新線程,內環在外環的線程中)
    //2021-05-25 21:54:17.658046+0800 GcdDemo[51591:6655010] --異步串行1: <NSThread: 0x6000033ad400>{number = 6, name = (null)}
    //2021-05-25 21:54:17.658177+0800 GcdDemo[51591:6655010] --異步串行2: <NSThread: 0x6000033ad400>{number = 6, name = (null)}
    dispatch_async(serialQueue, ^{
        NSLog(@"--異步串行1: %@",[NSThread currentThread]);
        dispatch_async(serialQueue, ^{
            NSLog(@"--異步串行2: %@",[NSThread currentThread]);
        });
    });
     
    
    //7. 同步串行,嵌套同一個同步串行隊列(內環死鎖卡崩潰)
    dispatch_sync(serialQueue, ^{
        NSLog(@"--同步串行1: %@",[NSThread currentThread]);
        dispatch_sync(serialQueue, ^{
            NSLog(@"--同步串行1: %@",[NSThread currentThread]);
        });
    });
    
    //8. 同步串行,嵌套異步串行隊列(外環不開啓線程,內環開啓線程)
    //2021-05-25 21:58:05.277050+0800 GcdDemo[52128:6660403] --同步串行1: <NSThread: 0x600000a30180>{number = 1, name = main}
    //2021-05-25 21:58:05.277239+0800 GcdDemo[52128:6660631] --異步串行2: <NSThread: 0x600000a65fc0>{number = 7, name = (null)}
    dispatch_sync(serialQueue, ^{
        NSLog(@"--同步串行1: %@",[NSThread currentThread]);
        dispatch_async(serialQueue, ^{
            NSLog(@"--異步串行2: %@",[NSThread currentThread]);
        });
    });
複製代碼
相關文章
相關標籤/搜索