iOS 多線程之任務和隊列
前言
學習多線程,肯定要了解GCD,GCD兩個最核心的概念就是:任務和隊列。所以學習好多線程,首先要把任務和隊列吃透,才能能好的使用多線程。
爲什麼使用GCD?
因爲使用 GCD 有很多好處啊,具體如下:
- GCD 可用於多核的並行運算;
- GCD 會自動利用更多的 CPU 內核(比如雙核、四核);
- GCD 會自動管理線程的生命週期(創建線程、調度任務、銷燬線程);
- 程序員只需要告訴 GCD 想要執行什麼任務,不需要編寫任何線程管理代碼。
任務
任務:就是執行操作的意思,換句話說就是你在線程中執行的那段代碼。在 GCD 中是放在 block 中的。
執行任務有兩種方式:
- 同步執行
- 同步添加任務到指定的隊列中,在添加的任務執行結束之前,會一直等待,直到隊列裏面的任務完成之後再繼續執行。
- 只能在當前線程中執行任務,不具備開啓新線程的能力。
- 異步執行
- 異步添加任務到指定的隊列中,它不會做任何等待,可以繼續執行任務。
- 可以在新的線程中執行任務,具備開啓新線程的能力。
兩者的主要區別是:是否等待隊列的任務執行結束,以及是否具備開啓新線程的能力。
隊列
這裏的隊列指執行任務的等待隊列,即用來存放任務的隊列。
-
隊列是一種特殊的線性表
-
採用 FIFO(先進先出)的原則,即新任務總是被插入到隊列的末尾,而讀取任務的時候總是從隊列的頭部開始讀取。
-
每讀取一個任務,則從隊列中釋放一個任務。隊列的結構可參考下圖:
隊列有兩種:
-
串行隊列(Serial Dispatch Queue)
- 每次只有一個任務被執行。讓任務一個接着一個地執行。(只開啓一個線程,一個任務執行完畢後,再執行下一個任務)
-
並行隊列 (Concurrent Dispatch Queue)
- 可以讓多個任務併發(同時)執行。(可以開啓多個線程,並且同時執行任務)
注意:併發隊列 的併發功能只有在異步(dispatch_async)方法下才有效。
GCD使用步驟
GCD 的使用步驟其實很簡單隻有兩步:
- 首先創建一個隊列(串行隊列或併發隊列);
- 最後將任務追加到任務的等待隊列中,然後系統就會根據任務類型執行任務(同步執行或異步執行)
隊列使用
可以使用 dispatch_queue_create 方法來創建隊列。
- 第一個參數表示隊列的唯一標識符,用於 DEBUG,可爲空。隊列的名稱推薦使用應用程序 ID 這種逆序全程域名。
- 第二個參數用來識別是串行隊列還是併發隊列。DISPATCH_QUEUE_SERIAL 表示串行隊列,DISPATCH_QUEUE_CONCURRENT 表示併發隊列。
串行隊列創建
-
創建串行隊列
// 串行隊列的創建方法 dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL);
-
系統提供串行隊列-主隊列(Main Dispatch Queue)
// 主隊列的獲取方法 dispatch_queue_t queue = dispatch_get_main_queue();
-
所有放在主隊列中的任務,都會放到主線程中執行。
-
系統提供dispatch_get_main_queue() 方法獲得主隊列。
注意:主隊列其實並不特殊。 主隊列的實質上就是一個普通的串行隊列,只是因爲默認情況下,當前代碼是放在主隊列中的,然後主隊列中的代碼,有都會放到主線程中去執行,所以才造成了主隊列特殊的現象。
並行隊列創建
-
創建並行隊列
// 併發隊列的創建方法 dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
-
系統提供的-全局併發隊列(Global Dispatch Queue)
// 全局併發隊列的獲取方法 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); ```
-
可以使用 dispatch_get_global_queue 方法來獲取全局併發隊列。
-
需要傳入兩個參數。第一個參數表示隊列優先級,一般用 DISPATCH_QUEUE_PRIORITY_DEFAULT。第二個參數暫時沒用,用 0 即可。
創建任務方法
-
同步執行任務
// 同步執行任務創建方法 dispatch_sync(queue, ^{ // 這裏放同步執行任務代碼 });
-
異步執行任務
// 異步執行任務創建方法 dispatch_async(queue, ^{ // 這裏放異步執行任務代碼 });
任務和隊列的組合
既然我們有兩種隊列(串行隊列 / 併發隊列),兩種任務執行方式(同步執行 / 異步執行),那麼我們就有了四種不同的組合方式。
這四種不同的組合方式是。
兩種默認隊列:全局併發隊列、主隊列:
-
全局併發隊列可以作爲普通併發隊列來使用。
-
當前代碼默認放在主隊列中,所以主隊列很有必要專門來研究一下,所以我們就又多了兩種組合方式。
這樣就有六種不同的組合方式了。
- 同步執行 + 串行隊列
- 同步執行 + 並行隊列
- 異步執行 + 串行隊列
- 異步執行 + 並行隊列
- 同步執行 + 主隊列
- 異步執行 + 主隊列
1.同步執行+串行隊列
- (void)syncSerial {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當前線程
NSLog(@"syncSerial---begin");
//創建串行隊列
dispatch_queue_t queue = dispatch_queue_create("testSerialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
// 模擬耗時操作
[NSThread sleepForTimeInterval:2];
// 打印當前線程
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"syncSerial---end");
}
執行打印結果:
輸出結果:
currentThread---<NSThread: 0x604000079400>{number = 1, name = main}
syncSerial---begin
1---<NSThread: 0x604000079400>{number = 1, name = main}
1---<NSThread: 0x604000079400>{number = 1, name = main}
2---<NSThread: 0x604000079400>{number = 1, name = main}
2---<NSThread: 0x604000079400>{number = 1, name = main}
3---<NSThread: 0x604000079400>{number = 1, name = main}
3---<NSThread: 0x604000079400>{number = 1, name = main}
syncSerial---end
同步+串行的特點:不會開啓新線程,在當前線程執行任務。任務是串行的,執行完一個任務,再執行下一個任務。
2.同步執行+並行隊列
- (void)syncConcurrent {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當前線程
NSLog(@"syncConcurrent---begin");
//創建並行隊列
dispatch_queue_t queue = dispatch_queue_create("testConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
// 模擬耗時操作
[NSThread sleepForTimeInterval:2];
// 打印當前線程
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"syncConcurrent---end");
}
打印結果:
currentThread---<NSThread: 0x60400006bbc0>{number = 1, name = main}
syncConcurrent---begin
1---<NSThread: 0x60400006bbc0>{number = 1, name = main}
1---<NSThread: 0x60400006bbc0>{number = 1, name = main}
2---<NSThread: 0x60400006bbc0>{number = 1, name = main}
2---<NSThread: 0x60400006bbc0>{number = 1, name = main}
3---<NSThread: 0x60400006bbc0>{number = 1, name = main}
3---<NSThread: 0x60400006bbc0>{number = 1, name = main}
syncConcurrent---end
打印結果表明:
在當前線程中執行任務,不會開啓新線程,執行完一個任務,再執行下一個任務。
執行完同步+串行、同步+並行結果表明:
- 同步情況串行和並行執行結果一樣
- 同步不具有開啓新線程的能力
- 並行隊列雖然後並行執行任務的能力,但是在同步的情況下,沒有開啓新線程的能力,所以相當於串行執行(並行在同步的情況下,難免想起英雄無用武之地)。
3.異步執行+串行隊列
- (void)asyncSerial {
//打印當前線程
NSLog(@"currentThread---%@",[NSThread currentThread]); 打印當前線程
NSLog(@"asyncSerial---begin");
dispatch_queue_t queue = dispatch_queue_create("testSerialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"asyncSerial---end");
}
打印結果:
currentThread---<NSThread: 0x604000070440>{number = 1, name = main}
asyncSerial---begin
asyncSerial---end
1---<NSThread: 0x60000026e100>{number = 3, name = (null)}
1---<NSThread: 0x60000026e100>{number = 3, name = (null)}
2---<NSThread: 0x60000026e100>{number = 3, name = (null)}
2---<NSThread: 0x60000026e100>{number = 3, name = (null)}
3---<NSThread: 0x60000026e100>{number = 3, name = (null)}
3---<NSThread: 0x60000026e100>{number = 3, name = (null)}
直接結果表明:異步情況下會開啓新線程,但是因爲任務是串行的,執行完一個任務,再執行下一個任務。
4.異步執行+並行隊列
- (void)asyncConcurrent {
// 打印當前線程
NSLog(@"currentThread---%@",[NSThread currentThread]);
NSLog(@"asyncConcurrent---begin");
dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"asyncConcurrent---end");
}
執行打印結果:
currentThread---<NSThread: 0x604000062d80>{number = 1, name = main}
asyncConcurrent---begin
2---<NSThread: 0x604000266f00>{number = 5, name = (null)}
3---<NSThread: 0x60000026f200>{number = 4, name = (null)}
1---<NSThread: 0x600000264800>{number = 3, name = (null)}
3---<NSThread: 0x60000026f200>{number = 4, name = (null)}
1---<NSThread: 0x600000264800>{number = 3, name = (null)}
2---<NSThread: 0x604000266f00>{number = 5, name = (null)}
執行結果表明:可以開啓多個子線程,任務同時並行交替執行
在異步執行任務的情況,串行隊列和並行隊列表明:
- 異步情況下是開啓新線程
- 異步下的串行也是是執行完一個任務,再執行下一個任務
- 異步下的並行開啓多個子線程同時交替執行多個任務
4.同步執行+主隊列
- (void)syncMain {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當前線程
NSLog(@"syncMain---begin");
dispatch_queue_t syncMain = dispatch_get_main_queue();
dispatch_sync(syncMain, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_sync(syncMain, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_sync(syncMain, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"syncMain---end");
}
執行結果:
currentThread---<NSThread: 0x600000078a00>{number = 1, name = main}
syncMain---begin
直接結果表明:互等卡主不執行,產生死鎖崩潰。
默認主線程在等待syncMain執行完任務1再往下執行,syncMain在等待默認主線程執行完再執行syncMain中任務1,所以互相等待產生死鎖。
6.異步執行+主隊列
- (void)asyncMain {
// 打印當前線程
NSLog(@"currentThread---%@",[NSThread currentThread]);
NSLog(@"asyncMain---begin");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
// 追加任務1
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務2
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
// 追加任務3
for (int i = 0; i < 2; ++i) {
[NSThread sleepForTimeInterval:2];
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"asyncMain---end");
}
執行打印結果:
currentThread---<NSThread: 0x60000006d440>{number = 1, name = main}
asyncMain---begin
asyncMain---end
1---<NSThread: 0x60000006d440>{number = 1, name = main}
1---<NSThread: 0x60000006d440>{number = 1, name = main}
2---<NSThread: 0x60000006d440>{number = 1, name = main}
2---<NSThread: 0x60000006d440>{number = 1, name = main}
3---<NSThread: 0x60000006d440>{number = 1, name = main}
3---<NSThread: 0x60000006d440>{number = 1, name = main}
因爲主線程是串行隊列,所以在主線程中執行任務,執行完一個任務,再執行下一個任務。
總結
-
串行隊列的特點:
- 無論同步任務或是異步任務,任務按順序執行,一個執行完畢執行下一個任務
- 串行隊列 執行同步任務不開闢線程
- 執行異步任務開闢最多開闢一條線程並且按順序執行
-
並行隊列的特點:
- 執行異步任務具備開闢多條線程的能力
- 執行同步任務,順序執行,因爲同步不具有開闢線程的能力
-
同步任務特點:
- 沒有開啓新線程的能力
- 在當前線程任務按順序一個一個執行
-
異步任務的特點:
- 具有開啓新線程的能力
- 開幾條新線程取決於隊列,串行隊列開啓一條線程,並行隊列在執行多個異步任務時會開闢多條線程。