1. 隊列創(chuàng)建
/**
隊列創(chuàng)建
dispatch_queue_create()
第一個參數(shù)表示隊列的唯一標識符,用于 DEBUG,可為空
第二個參數(shù) DISPATCH_QUEUE_SERIAL表示串行隊列,DISPATCH_QUEUE_CONCURRENT表示并發(fā)隊列。
*/
- (void)createQueue{
// 串行隊列創(chuàng)建
dispatch_queue_t serialQueue = dispatch_queue_create("serial.kelly.com", DISPATCH_QUEUE_SERIAL);
// 并發(fā)隊列創(chuàng)建
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
// 獲取主隊列:所有放在主隊列中的任務,都會放到主線程中執(zhí)行。
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// 對于并發(fā)隊列,GCD 默認提供了全局并發(fā)隊列(Global Dispatch Queue)
// 第一個參數(shù)優(yōu)先級,第二個填0即可
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}
2. 線程組合運用
2.1 同步執(zhí)行 + 并發(fā)隊列
特點:在當前線程中執(zhí)行任務,不會開啟新線程,執(zhí)行完一個任務,再執(zhí)行下一個任務。
- (void)syncAndConcurrent{
NSLog(@"當前線程= %@",[NSThread currentThread]);
NSLog(@"syncAndConcurrent begin ---");
dispatch_queue_t queue = dispatch_queue_create("syncAndConcurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
// 模擬耗時2s
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndConcurrent --> 1 當前線程=%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
// 模擬耗時2s
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndConcurrent --> 2 當前線程=%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
// 模擬耗時2s
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndConcurrent --> 3 當前線程=%@",[NSThread currentThread]);
});
NSLog(@"syncAndConcurrent end ---");
}

1533817936498.jpg
2.2 異步執(zhí)行 + 并發(fā)隊列
特點:可以開啟多個線程,任務交替(同時)執(zhí)行。
- (void)asyncAndConcurrent{
NSLog(@"當前線程= %@",[NSThread currentThread]);
NSLog(@"asyncAndConcurrent begin ---");
dispatch_queue_t queue = dispatch_queue_create("asyncAndConcurrent.kelly.com", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndConcurrent --> 1 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndConcurrent --> 2 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndConcurrent --> 3 當前線程=%@",[NSThread currentThread]);
});
NSLog(@"asyncAndConcurrent end ---");
}

1533818173721.jpg
2.3 同步執(zhí)行 + 串行隊列
特點:不會開啟新線程,在當前線程執(zhí)行任務。任務是串行的,執(zhí)行完一個任務,再執(zhí)行下一個任務。
- (void)syncAndSerial{
NSLog(@"當前線程= %@",[NSThread currentThread]);
NSLog(@"syncAndSerial begin ---");
dispatch_queue_t queue = dispatch_queue_create("syncAndSerial.kelly.com", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndSerial --> 1 當前線程=%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndSerial --> 2 當前線程=%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndSerial --> 3 當前線程=%@",[NSThread currentThread]);
});
NSLog(@"syncAndSerial end ---");
}

1533818397023.jpg
2.4 異步執(zhí)行 + 串行隊列
會開啟新線程,但是因為任務是串行的,執(zhí)行完一個任務,再執(zhí)行下一個任務。
- (void)asyncAndSerial{
NSLog(@"當前線程= %@",[NSThread currentThread]);
NSLog(@"asyncAndSerial begin ---");
dispatch_queue_t queue = dispatch_queue_create("asyncAndSerial.kelly.com", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndSerial --> 1 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndSerial --> 2 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"asyncAndSerial --> 3 當前線程=%@",[NSThread currentThread]);
});
NSLog(@"asyncAndSerial end ---");
}

1533818515599.jpg
2.5 異步執(zhí)行 + 主隊列
特點:只在主線程中執(zhí)行任務,執(zhí)行完一個任務,再執(zhí)行下一個任務
- (void)asyncAndMain{
NSLog(@"當前線程---%@",[NSThread currentThread]); // 打印當前線程
NSLog(@"asyncAndMain---begin");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndMain --> 1 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndMain --> 2 當前線程=%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"syncAndMain --> 3 當前線程=%@",[NSThread currentThread]);
});
NSLog(@"asyncAndMain---end");
}

1533818828690.jpg
2.6 線程間通信
- (void)communication{
// 獲取全局并發(fā)隊列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 獲取主隊列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// 異步追加任務
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdCommunication --> 1 當前線程=%@",[NSThread currentThread]);
// 回到主線程
dispatch_async(mainQueue, ^{
// 刷新UI等操作
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdCommunication --> 2 當前線程=%@",[NSThread currentThread]);
});
});
}
3. 多線程其他方法
3.1 柵欄方法 dispatch_barrier_async
dispatch_barrier_async函數(shù)會等待前邊追加到并發(fā)隊列中的任務全部執(zhí)行完畢之后,再將指定的任務追加到該異步隊列中。然后在dispatch_barrier_async函數(shù)追加的任務執(zhí)行完畢之后,異步隊列才恢復為一般動作,接著追加任務到該異步隊列并開始執(zhí)行。
- (void)gcdAndBarrier{
dispatch_queue_t queue = dispatch_queue_create("gcdAndBarrier.kelly.com", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"gcdAndBarrier start");
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdAndBarrier -- > 1 當前線程%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdAndBarrier -- > 2 當前線程%@",[NSThread currentThread]);
});
// 追加barrier
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdAndBarrier -- > 3 當前線程%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdAndBarrier -- > 4 當前線程%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"gcdAndBarrier -- > 5 當前線程%@",[NSThread currentThread]);
});
}

1533819192522.jpg
3.2 GCD 延時
- (void)gcdAndDelay{
NSLog(@"currentThread---%@",[NSThread currentThread]);
NSLog(@"gcdAndDelay---begin");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
// 2.0秒后異步追加任務代碼到主隊列,并開始執(zhí)行
NSLog(@"after---%@",[NSThread currentThread]);
});
NSLog(@"gcdAndDelay---end");
}
3.3 隊列組 dispatch_group_notify
- (void)groupNotify {
NSLog(@"currentThread---%@",[NSThread currentThread]);
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"groupNotify 1---%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"groupNotify 2---%@",[NSThread currentThread]);
});
// 等前面的異步任務1、任務2都執(zhí)行完畢后,回到主線程執(zhí)行下邊任務
dispatch_group_notify(group, mainQueue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"groupNotify 3---%@",[NSThread currentThread]);
NSLog(@"group---end");
});
}
3.4 dispatch_group_wait
暫停當前線程(阻塞當前線程),等待指定的 group 中的任務執(zhí)行完成后,才會往下繼續(xù)執(zhí)行。
- (void)groupWait {
NSLog(@"groupWait---%@",[NSThread currentThread]);
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"groupWait 1---%@",[NSThread currentThread]);
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"groupWait 2---%@",[NSThread currentThread]);
});
// 等待上面的任務全部完成后,會往下繼續(xù)執(zhí)行(會阻塞當前線程)
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"group---end");
}

1533819498842.jpg
今天暫時寫這么多,有什么不足的地方,希望各位指出來大家一起學習。