GCD有三種queue
main queue: 主線程隊列。是一個串行隊列。一般用來更新UI。
global queue: 全局隊列,是一個并行隊列。使用方法相信大家都知道。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
[self ? ? test];
}
上面的意思就是開啟一個異步線程,在全局隊列中執行。
custom queue:自定義隊列。有兩種自定義隊列?!?/p>
dispatch_queue_t serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrent_queue = dispatch_queue_create("com.concurrent.www", DISPATCH_QUEUE_CONCURRENT);
serial_queue即是我自定義的一個串行隊列。上面提到的主線程隊列也是一個串行隊列。
concurrent_queue是我自定義的一個并行隊列。上面提到的global queue就是一個并行隊列。
現在我們來討論2個問題。
1.dispatch_async里使用串行隊列和并行隊列的效果。
2.dispatch_sync里使用串行隊列和并行隊列的效果。
串行隊列和并行隊列的創建如下。
serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);
concurrent_queue = dispatch_queue_create("com.LiSiGuang.www", DISPATCH_QUEUE_CONCURRENT);
討論的問題:
“在dispatch_async使用串行隊列”
代碼如下:
-(void)testSerialQueueWithAsync
{
? ? for ( int index =0; index < 10; index++)
? ? {
? ? ? ? dispatch_async(serial_queue,^{
? ? ? ? ? ? NSLog(@"index =%d",index);
? ? ? ? ? ? NSLog(@" current thread is %@",[NSThread currentThread]);
? ? ? ? });
????? }
? ? ? ? ? NSLog(@"Running on main Thread");
}
然后在viewDidLoad()方法里打印,打印結果如下。
index =0
Running on main Thread
current thread is<NSThred:0x600000466b40>{number = 4, name = (null)}
index =1
current thread is{number = 4, name = (null)}
index =2
current thread is{number = 4, name = (null)}
index =3
current thread is{number = 4, name = (null)}
index =4
current thread is{number = 4, name = (null)}
index =5
current thread is{number = 4, name = (null)}
index =6
current thread is{number = 4, name = (null)}
index =7
current thread is{number = 4, name = (null)}
index =8
current thread is{number = 4, name = (null)}
index =9
current thread is{number = 4, name = (null)}
打印結果的幾個特征。
在dispatch_async使用的所有Thread均為同一個Thread。因為他們的指針地址完全相同。
輸出結果是按順序輸出,符合我們對串行隊列的期待。即FIFO。先進先出原則。
Running on main Thread這句話并沒有在最后執行,而是會出現在隨機的位置,這也符合我們對dispatch_async的期待,因為他會開辟一個新的線程執行,不會阻塞主線程。 ok,讓我們測試下一個。
在dispatch_async中使用并行隊列
2018-01-31 10:07:40.409923+0800 TextD[22242:494553] index =22018-01-31 10:07:40.409922+0800 TextD[22242:494552] index =02018-01-31 10:07:40.409922+0800 TextD[22242:494551] index =12018-01-31 10:07:40.409922+0800 TextD[22242:494487] Running on main Thread2018-01-31 10:07:40.409980+0800 TextD[22242:494554] index =32018-01-31 10:07:40.410040+0800 TextD[22242:494561] index =42018-01-31 10:07:40.410284+0800 TextD[22242:494551] current thread is{number = 3, name = (null)}2018-01-31 10:07:40.410285+0800 TextD[22242:494552] current thread is{number = 5, name = (null)}2018-01-31 10:07:40.410292+0800 TextD[22242:494553] current thread is{number = 4, name = (null)}2018-01-31 10:07:40.410349+0800 TextD[22242:494554] current thread is{number = 6, name = (null)}2018-01-31 10:07:40.410422+0800 TextD[22242:494551] index =52018-01-31 10:07:40.410435+0800 TextD[22242:494561] current thread is{number = 7, name = (null)}2018-01-31 10:07:40.410780+0800 TextD[22242:494562] index =62018-01-31 10:07:40.410881+0800 TextD[22242:494564] index =82018-01-31 10:07:40.410882+0800 TextD[22242:494563] index =72018-01-31 10:07:40.410929+0800 TextD[22242:494565] index =92018-01-31 10:07:40.412670+0800 TextD[22242:494551] current thread is{number = 3, name = (null)}2018-01-31 10:07:40.437759+0800 TextD[22242:494563] current thread is{number = 9, name = (null)}2018-01-31 10:07:40.437768+0800 TextD[22242:494562] current thread is{number = 8, name = (null)}2018-01-31 10:07:40.437787+0800 TextD[22242:494564] current thread is{number = 10, name = (null)}2018-01-31 10:07:40.437830+0800 TextD[22242:494565] current thread is{number = 11, name = (null)}
打印結果的特征如下:
輸出的結果是亂序的,說明我們的輸出語句是并發的,由多個線程共同執行的。
Running on main Thread這句話依然沒有被阻塞,直接輸出了。
每次打印語句的Thread均不相同。
仔細比對兩次打印結果的異同點。提出問題。
串行隊列如何保證在異步線程中遵守先進先出原則(從Demo里看,也就是順序打印我們的結果)?
很簡單,它會保證每次dispatch_async開辟線程執行串行隊列中的任務時,總是使用同一個異步線程。這也是為什么我們的第一次打印結果中,NSThread總是同一個。
在dispatch_async中放入并行隊列并執行的時候,為什么執行順序總是亂序的?
因為在并行對列中,每執行一次任務的時候,dispatch_async總會為我們開辟一個新的線程(當然,開辟線程的[…]”
在dispatch_sync使用串行隊列:
-(void)testSerialQueueWithsync
{ ? ?dispatch_queue_t serial_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_SERIAL);
? ? for ( int index =0; index < 10; index++)
? ? { ? ? ? ?dispatch_sync(serial_queue,^{
? ? ? ? ? ? NSLog(@"index =%d",index);
?? ? ? ? ? ?NSLog(@" current thread is %@",[NSThread currentThread]);
? ? ? ? });?
? }
? ? NSLog(@"Running on main Thread");
}
1.dispatch_sync并沒有開辟一個新的線程,直接在當前線程中執行代碼(即main線程)。所以會阻塞當前線程。
2.Running on main Thread在最后輸出。
也就是說,當使用dispatch_sync執行串行隊列的任務時,不會開辟新的線程,會直接使用當前線程執行隊列中的任務。
在dispatch_sync中使用并行隊列
-(void)testConcurrentQueueWithsync
{ ? ?dispatch_queue_t Concurrent_queue = dispatch_queue_create("com.reviewcode.www", DISPATCH_QUEUE_CONCURRENT);
? ? for ( int index =0; index < 10; index++)
? ? {
? ? ? ? dispatch_sync(Concurrent_queue,^{
? ? ? ? ? ? NSLog(@"index =%d",index);
? ? ? ? ? ? NSLog(@" current thread is %@",[NSThread currentThread]);
? ? ? ? });
?? ?}
?? ?NSLog(@"Running on main Thread");
}
打印結果如下:
2018-01-31 10:30:19.728025+0800 TextD[22520:524950] index =02018-01-31 10:30:19.728294+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728425+0800 TextD[22520:524950] index =12018-01-31 10:30:19.728605+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728753+0800 TextD[22520:524950] index =22018-01-31 10:30:19.728875+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.728990+0800 TextD[22520:524950] index =32018-01-31 10:30:19.729108+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729223+0800 TextD[22520:524950] index =42018-01-31 10:30:19.729351+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729474+0800 TextD[22520:524950] index =52018-01-31 10:30:19.729597+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729697+0800 TextD[22520:524950] index =62018-01-31 10:30:19.729831+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.729952+0800 TextD[22520:524950] index =72018-01-31 10:30:19.730147+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.730316+0800 TextD[22520:524950] index =82018-01-31 10:30:19.730617+0800 TextD[22520:524950] current thread is{number = 1, name = main}2018-01-31 10:30:19.730786+0800 TextD[22520:524950] index =92018-01-31 10:30:19.730986+0800 TextD[22520:524950] current thread is{number = 1, name = main}
2018-01-31 10:30:19.731201+0800 TextD[22520:524950] Running on main Thread
總結:
結果很奇怪,和在串行隊列執行的效果一模一樣。按我們的思考,并行隊列里執行任務不應該是多個線程同時跑么?其實是由于dispatch_sync并不會開辟新的線程執行任務,所以導致了執行并行隊列任務的線程總會是一個線程,自然,結果是一樣的。