關(guān)于GCD的最最基本的知識,往往很多初學(xué)者都被忽略浑槽。理解上造成了偏差蒋失,而不注重去實(shí)踐, 所有我們需要認(rèn)真的總結(jié)一下括荡。
dispatch_asyn和dispatch_sync添加任務(wù)到dispatch隊(duì)列時高镐,是否創(chuàng)建線程呢,那么創(chuàng)建線程是創(chuàng)建一個呢還是多個呢畸冲,如果你自己能直接分的清楚就不必看我下文的實(shí)踐代碼了嫉髓, 下文的代碼做了詳細(xì)的說明观腊, 不僅僅是代碼,也包含了總結(jié)算行, 如果你想實(shí)踐的話梧油, 也可以跟著實(shí)踐一遍哦,這樣最好哦
以下主要總結(jié)了隊(duì)列的類型和隊(duì)列的堵塞
- (void)viewDidLoad {
[super viewDidLoad];
//********
[self blockIntroduced];
//********通過以上測試我們得出如下幾個結(jié)論
/*
1.dispatch_sync添加任務(wù)到隊(duì)列州邢,不會創(chuàng)建新的線程都是在當(dāng)前線程中處理的碰镜。無論添加到串行隊(duì)列里或者并行隊(duì)列里,都是串行效果肛鹏,因?yàn)檫@個方法是等任務(wù)執(zhí)行完成以后才會返回飞涂。
2.dispatch_async添加任務(wù)到
2-1:mainQueue不創(chuàng)建線程,在主線程中串行執(zhí)行
2-2:globalQueue 和 并行隊(duì)列:根據(jù)任務(wù)系統(tǒng)決定開辟線程個數(shù)
2-3:串行對列:創(chuàng)建一個線程:串行執(zhí)行呀枢。
*/
[self blockedMainThread0];
[self blockedMainThread1];
[self blockedMainThread2];
[self blockedMainThread3];
// [self blockedMainThread];
}
-(void)blockIntroduced
{
//隊(duì)列一般就是系統(tǒng)的主隊(duì)列和全局隊(duì)列還有自己手動創(chuàng)建的串行隊(duì)列和并行隊(duì)列
dispatch_queue_t chuanxingduilie = dispatch_queue_create("chuanxingduilie", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t bingxingduilie = dispatch_queue_create("bingxingduilie", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
//方法有兩個 dispatch_sync 和 dispatch_async dispatch_async方法是立刻返回的胚股,也就是說把block內(nèi)容加到相應(yīng)隊(duì)列里后會立馬返回,而dispatch_sync要等到加到隊(duì)列里執(zhí)行完之后才會返回裙秋。
//這時候總共有2*4 = 8種組合琅拌,接下來我們來說一下這八種組合
//一:
for(int i=0;i<3;i++){
dispatch_sync(chuanxingduilie, ^{
NSLog(@"dispatch_sync-chuanxingduilie-%d\n mainThread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 21:07:12.622 GcdTest[7229:164997] dispatch_sync-chuanxingduilie-0
mainThread:<NSThread: 0x7fad93407dd0>{number = 1, name = main}
2016-02-05 21:07:12.623 GcdTest[7229:164997] dispatch_sync-chuanxingduilie-1
mainThread:<NSThread: 0x7fad93407dd0>{number = 1, name = main}
2016-02-05 21:07:12.623 GcdTest[7229:164997] dispatch_sync-chuanxingduilie-2
mainThread:<NSThread: 0x7fad93407dd0>{number = 1, name = main}
*/
//第一種方式可見,串行隊(duì)列是在主線程里完成的摘刑,因?yàn)槭谴嘘?duì)列进宝,所以打印%d是有順序的。
//二:
for(int i=0;i<3;i++){
dispatch_sync(bingxingduilie, ^{
NSLog(@"dispatch_sync-bingxingduilie-%d\n mainThread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 21:09:26.422 GcdTest[7241:165846] dispatch_sync-bingxingduilie-0
mainThread:<NSThread: 0x7face9d01d20>{number = 1, name = main}
2016-02-05 21:09:26.422 GcdTest[7241:165846] dispatch_sync-bingxingduilie-1
mainThread:<NSThread: 0x7face9d01d20>{number = 1, name = main}
2016-02-05 21:09:26.422 GcdTest[7241:165846] dispatch_sync-bingxingduilie-2
mainThread:<NSThread: 0x7face9d01d20>{number = 1, name = main}
*/
//第二種方式可以發(fā)現(xiàn)同樣還是在主線程里執(zhí)行并行隊(duì)列枷恕,(雖然是并行隊(duì)列党晋,但這時候依然在同一個線程里執(zhí)行)
//三:
/*
for(int i=0;i<3;i++){
NSLog(@"dispatch_sync-mainQueue");
dispatch_sync(mainQueue, ^{
NSLog(@"dispatch_sync-mainQueue-%d\n mainThread:%@",i,[NSThread currentThread]);
});
}
//打印結(jié)果:dispatch_sync-mainQueue,發(fā)現(xiàn)主線程被堵塞活尊。原因在堵塞中分析隶校。此種方式暫時先屏蔽掉
*/
//四:
for(int i=0;i<3;i++){
dispatch_sync(globalQueue, ^{
NSLog(@"dispatch_sync-globalQueue-%d\n mainThread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 21:22:41.107 GcdTest[7338:172870] dispatch_sync-globalQueue-0
mainThread:<NSThread: 0x7feadad050b0>{number = 1, name = main}
2016-02-05 21:22:41.107 GcdTest[7338:172870] dispatch_sync-globalQueue-1
mainThread:<NSThread: 0x7feadad050b0>{number = 1, name = main}
2016-02-05 21:22:41.107 GcdTest[7338:172870] dispatch_sync-globalQueue-2
mainThread:<NSThread: 0x7feadad050b0>{number = 1, name = main}
*/
//打印結(jié)果可以看出依然是主線程。
//五:
for (int i=0; i<3; i++) {
dispatch_async(chuanxingduilie, ^{
if (i==1) {
sleep(2);
}
NSLog(@"dispatch_async-chuanxingduilie-%d\n Thread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 22:32:14.957 GcdTest[7499:191789] dispatch_async-chuanxingduilie-0
Thread:<NSThread: 0x7fd7eaf10c70>{number = 3, name = (null)}
2016-02-05 22:32:16.962 GcdTest[7499:191789] dispatch_async-chuanxingduilie-1
Thread:<NSThread: 0x7fd7eaf10c70>{number = 3, name = (null)}
2016-02-05 22:32:16.963 GcdTest[7499:191789] dispatch_async-chuanxingduilie-2
Thread:<NSThread: 0x7fd7eaf10c70>{number = 3, name = (null)}
*/
//可以看出創(chuàng)建了一個線程蛹锰, 在串行隊(duì)列里串行執(zhí)行的
//六:
for (int i=0; i<3; i++) {
dispatch_async(bingxingduilie, ^{
if (i==1) {
sleep(3);
}
NSLog(@"dispatch_async-bingxingduilie-%d\n Thread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 22:10:07.083 GcdTest[7425:184003] dispatch_async-bingxingduilie-2
Thread:<NSThread: 0x7fcda24bcd00>{number = 4, name = (null)}
2016-02-05 22:10:07.083 GcdTest[7425:183982] dispatch_async-bingxingduilie-0
Thread:<NSThread: 0x7fcda2502310>{number = 3, name = (null)}
2016-02-05 22:10:10.083 GcdTest[7425:184002] dispatch_async-bingxingduilie-1
Thread:<NSThread: 0x7fcda2436180>{number = 5, name = (null)}
*/
//分析可以看出創(chuàng)建了多個線程深胳,任務(wù)執(zhí)行順序不一定(時間差不多的話)
//七:
for (int i=0; i<3; i++) {
dispatch_async(mainQueue, ^{
if (i==1) {
sleep(3);
}
NSLog(@"dispatch_async-mainQueue-%d\n Thread:%@",i,[NSThread currentThread]);
});
}
/*
Thread:<NSThread: 0x7fbb28704510>{number = 5, name = (null)}
2016-02-05 22:15:03.709 GcdTest[7460:186485] dispatch_async-mainQueue-0
Thread:<NSThread: 0x7fbb28703fb0>{number = 1, name = main}
2016-02-05 22:15:06.710 GcdTest[7460:186485] dispatch_async-mainQueue-1
Thread:<NSThread: 0x7fbb28703fb0>{number = 1, name = main}
2016-02-05 22:15:06.710 GcdTest[7460:186485] dispatch_async-mainQueue-2
Thread:<NSThread: 0x7fbb28703fb0>{number = 1, name = main}
*/
//可以看出這種情況還是在當(dāng)前線程環(huán)境中執(zhí)行,并不創(chuàng)建線程铜犬,因?yàn)槭窃谥麝?duì)列里舞终,順序執(zhí)行
//八:
for (int i=0; i<3; i++) {
dispatch_async(globalQueue, ^{
NSLog(@"dispatch_async-globalQueue-%d\n Thread:%@",i,[NSThread currentThread]);
});
}
/*
2016-02-05 22:17:58.306 GcdTest[7482:188067] dispatch_async-globalQueue-1
Thread:<NSThread: 0x7fa281e24580>{number = 4, name = (null)}
2016-02-05 22:17:58.306 GcdTest[7482:188099] dispatch_async-globalQueue-2
Thread:<NSThread: 0x7fa281e321c0>{number = 5, name = (null)}
2016-02-05 22:17:58.307 GcdTest[7482:188069] dispatch_async-globalQueue-0
Thread:<NSThread: 0x7fa281e262b0>{number = 3, name = (null)}
*/
//可以看出創(chuàng)建了多個線程,執(zhí)行順序并不一定
}
-(void)blockedMainThread
{
//打印結(jié)果是1 分析:mainQueue里存在任務(wù)1癣猾,同步線程任務(wù)敛劝,這兩個任務(wù),當(dāng)執(zhí)行dispatch_sync時纷宇,把打印任務(wù)2加入主隊(duì)列夸盟,想要打印2必須等之前所有的任務(wù)都執(zhí)行完成,這時候因?yàn)橹麝?duì)列里有同步線程任務(wù)像捶,這時候相當(dāng)于自己在等自己執(zhí)行完成上陕,進(jìn)入死循環(huán)桩砰。
NSLog(@"1"); // 任務(wù)1
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"2"); // 任務(wù)2
});
NSLog(@"3"); // 任務(wù)3
}
-(void)blockedMainThread0
{
//打印結(jié)果123 分析:mainQueue里存在任務(wù)1,同步線程任務(wù)释簿,當(dāng)執(zhí)行dispatch_sync時亚隅,拿到全局隊(duì)列,
NSLog(@"1"); // 任務(wù)1
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"2"); // 任務(wù)2
});
NSLog(@"3"); // 任務(wù)3
}
-(void)blockedMainThread1
{
/*
//執(zhí)行結(jié)果:
2016-02-05 22:42:30.661 GcdTest[7511:194820] 1
2016-02-05 22:42:30.662 GcdTest[7511:194820] 5
2016-02-05 22:42:30.662 GcdTest[7511:194929] 2 <NSThread: 0x7f9ca8709990>{number = 3, name = (null)}
*/
dispatch_queue_t queue = dispatch_queue_create("com.demo.serialQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"1"); // 任務(wù)1
dispatch_async(queue, ^{
NSLog(@"2 %@",[NSThread currentThread]); // 任務(wù)2
dispatch_sync(queue, ^{
NSLog(@"3"); // 任務(wù)3
});
NSLog(@"4"); // 任務(wù)4
});
NSLog(@"5"); // 任務(wù)5
}
-(void)blockedMainThread2
{
//1,5,2,3,4 可以看出在全局隊(duì)列里拿到住隊(duì)列同步執(zhí)行是沒有問題的庶溶。
NSLog(@"****************");
NSLog(@"1"); // 任務(wù)1
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"2"); // 任務(wù)2
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"3 %@",[NSThread currentThread]); // 任務(wù)3
//2016-02-05 22:51:00.044 GcdTest[7548:198417] 3 <NSThread: 0x7feb3b701be0>{number = 1, name = main}
});
NSLog(@"4"); // 任務(wù)4
});
NSLog(@"5"); // 任務(wù)5
}
-(void)blockedMainThread3
{
//執(zhí)行結(jié)果:11煮纵,44
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"11"); // 任務(wù)1
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"22"); // 任務(wù)2
});
NSLog(@"33"); // 任務(wù)3
});
NSLog(@"44"); // 任務(wù)4
while (1) {
}
NSLog(@"55"); // 任務(wù)5
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
文章未完待續(xù):待續(xù)內(nèi)容為dispatch里的大部分常用的方法,等我有時間了一定會更新上去偏螺, 如果大家喜歡文章行疏, 就關(guān)注哦~
有什么不明白的,可以直接評論回復(fù)套像,交流隘擎。
1.dispath_once
dispatch_once的作用就是只執(zhí)行一次,我們在寫單例的時候可以用到
+ (BWStatusBarOverlay *)shared {
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
2.dispatch_after
//延遲0.5s以后執(zhí)行
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.5);
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"doSomething");
});
3.dispatch_group
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, globalQueue, ^{
sleep(1);
NSLog(@"1");
});
dispatch_group_async(group, globalQueue, ^{
sleep(2);
NSLog(@"2");
});
dispatch_group_async(group, globalQueue, ^{
sleep(3);
NSLog(@"3");
});
dispatch_group_notify(group, globalQueue, ^{
NSLog(@"Over!");
});
4.dispatch_barrier_async
dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^(){
NSLog(@"dispatch-1");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"dispatch-2");
});
dispatch_barrier_async(concurrentQueue, ^(){
NSLog(@"dispatch-barrier-1+2");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"dispatch-3");
});
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"dispatch-barrier-1+2+3");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"dispatch-4");
});
最后不多說凉夯,需要看看蘋果官方文檔這里截圖看下關(guān)于GCD的東西
gcd-1.png
gcd-2.png
gcd-3.png
看官方的東西其實(shí)不會的東西太多太多,任重而道遠(yuǎn)采幌, 明天就回家過年嘍劲够, 祝愿每一個人新的一年都能對自己的目標(biāo)更近一步。