详解GCD<1>队列

GCD有三种queue

main queue: 主线程队列。是一个串行队列。一般用来更新UI。

global queue: 全局队列,是一个并行队列。使用方法相信大家都知道。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{

[self     test];

}

上面的意思就是开启一个异步线程,在全局队列中执行。

custom queue:自定义队列。有两种自定义队列。”

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并不会开辟新的线程执行任务,所以导致了执行并行队列任务的线程总会是一个线程,自然,结果是一样的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容