GCD温故而知新1

GCD 三中queue
1.1 main queue
1.2 global queue
1.3 自己创建 queue
1.3.1 自己创建 queue 分两种
1>串行 dispatch_queue_t serial_queue = dispatch_queue_create("lcc", DISPATCH_QUEUE_SERIAL);
2>并行 dispatch_queue_t serial_concurrent = dispatch_queue_create("lccc", DISPATCH_QUEUE_CONCURRENT);

线程 有两种
2.1 sync 同步
2.2 async 异步

//异步 主队列
-(void)asyncMainQueue{
dispatch_queue_t main_queue = dispatch_get_main_queue();

for (int i = 0; i<5; i++) {
    dispatch_async(main_queue, ^{
        NSLog(@"i = %d",i);
        NSLog(@"current thread = %@",[NSThread currentThread]);
    });
}
for (int i = 0; i<5; i++) {
    NSLog(@"=%@",[NSThread currentThread]);
}
}
2016-11-07 13:39:55.601 gcd[5411:330259] =<NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.602 gcd[5411:330259] =<NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.603 gcd[5411:330259] =<NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.603 gcd[5411:330259] =<NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.604 gcd[5411:330259] =<NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.611 gcd[5411:330259] i = 0
2016-11-07 13:39:55.611 gcd[5411:330259] current thread = <NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.612 gcd[5411:330259] i = 1
2016-11-07 13:39:55.612 gcd[5411:330259] current thread = <NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.612 gcd[5411:330259] i = 2
2016-11-07 13:39:55.613 gcd[5411:330259] current thread = <NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.613 gcd[5411:330259] i = 3
2016-11-07 13:39:55.613 gcd[5411:330259] current thread = <NSThread: 0x60800006b2c0>{number = 1, name = main}
2016-11-07 13:39:55.614 gcd[5411:330259] i = 4
2016-11-07 13:39:55.726 gcd[5411:330259] current thread = <NSThread: 0x60800006b2c0>{number = 1, name = main}

异步主队列 分析
异步是不阻塞我们的当前线程(主线程) 所以下面for循环的 NSLog(@"=%@",[NSThread currentThread]);先被打印.async和main_queue组合没有创建新的线程 还是在当前线程中(主线程)这个时候当前线程(主线程)有下面的for循环打印任务 所以要等下面的任务打印好 上面的for循环 才会打印

// 异步 全局队列
-(void)asyncGlobleQueue{
dispatch_queue_t globle_queue = dispatch_get_global_queue(0, 0);

for (int i = 0; i<5; i++) {
    dispatch_async(globle_queue, ^{
        NSLog(@"i = %d",i);
        NSLog(@"current thread = %@",[NSThread currentThread]);
    });
}
for (int i = 0; i<5; i++) {
    NSLog(@"+++%@",[NSThread currentThread]);
}
}
2016-11-07 13:52:18.783 gcd[5479:341111] i = 4
2016-11-07 13:52:18.783 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}
2016-11-07 13:52:18.783 gcd[5479:341092] i = 1
2016-11-07 13:52:18.783 gcd[5479:341094] i = 3
2016-11-07 13:52:18.783 gcd[5479:341091] i = 2
2016-11-07 13:52:18.783 gcd[5479:341109] i = 0
2016-11-07 13:52:18.783 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}
2016-11-07 13:52:18.783 gcd[5479:341111] current thread = <NSThread: 0x60800006aac0>{number = 3, name = (null)}
2016-11-07 13:52:18.784 gcd[5479:341094] current thread = <NSThread: 0x60800006b500>{number = 5, name = (null)}
2016-11-07 13:52:18.784 gcd[5479:341092] current thread =  <NSThread: 0x600000071b00>{number = 4, name = (null)}
2016-11-07 13:52:18.784 gcd[5479:341091] current thread = <NSThread: 0x600000071980>{number = 6, name = (null)}
2016-11-07 13:52:18.784 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}
2016-11-07 13:52:18.784 gcd[5479:341109] current thread = <NSThread: 0x6000000719c0>{number = 7, name = (null)}
2016-11-07 13:52:18.784 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}
2016-11-07 13:52:18.793 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}

异步 全局队列分析
首先 异步 没有阻塞当前线程(主线程) 所以打印的第二行 就已经打印了我们下面的for循环了根据打印的log可以清楚的看到 async和globle_queue创建了新的线程 解释一下打印的顺序打印第一行
2016-11-07 13:52:18.783 gcd[5479:341111] i = 4 这个时候创建了新的线程(非主线程)是在新的线程中打印的,不阻塞当前线程(主线程)
2016-11-07 13:52:18.783 gcd[5479:341056] +++<NSThread: 0x6000000658c0>{number = 1, name = main}就是在当前线程打印的上面开辟了一个新线程做他自己的事下面 当前线程 做自己的事 想象一下就是第一个for循环一下就走完了没循环一次(就可能)创建一个线程就结束循环了打印就是在这创建的各个线程中异步打印..所以上面 for循环的顺序和下面的就乱了

//异步 穿行队列
-(void)asyncSerialQueue{
dispatch_queue_t serial_queue = dispatch_queue_create("com.rea", DISPATCH_QUEUE_SERIAL);

for (int i = 0; i<10; i++) {
    dispatch_async(serial_queue, ^{
        NSLog(@"---i = %d",i);
        NSLog(@"current thread = %@",[NSThread currentThread]);
    });
}

for (int i = 0; i<5; i++) {
    NSLog(@"+++%@",[NSThread currentThread]);
}
}

2016-11-07 14:19:17.060 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
2016-11-07 14:19:17.060 gcd[5549:360461] ---i = 0
2016-11-07 14:19:17.061 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
2016-11-07 14:19:17.061 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.062 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
2016-11-07 14:19:17.062 gcd[5549:360461] ---i = 1
2016-11-07 14:19:17.062 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
2016-11-07 14:19:17.062 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.062 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
2016-11-07 14:19:17.062 gcd[5549:360461] ---i = 2
2016-11-07 14:19:17.062 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.062 gcd[5549:360461] ---i = 3
2016-11-07 14:19:17.062 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.063 gcd[5549:360461] ---i = 4
2016-11-07 14:19:17.078 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.078 gcd[5549:360461] ---i = 5
2016-11-07 14:19:17.078 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.078 gcd[5549:360461] ---i = 6
2016-11-07 14:19:17.078 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.078 gcd[5549:360461] ---i = 7
2016-11-07 14:19:17.079 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.079 gcd[5549:360461] ---i = 8
2016-11-07 14:19:17.079 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}
2016-11-07 14:19:17.079 gcd[5549:360461] ---i = 9
2016-11-07 14:19:17.080 gcd[5549:360461] current thread = <NSThread: 0x600000071080>{number = 3, name = (null)}

异步 穿行队列分析
异步 不会阻塞当前线程 所以第一行打印的是下面的for循环
2016-11-07 14:19:17.060 gcd[5549:360426] +++<NSThread: 0x608000066280>{number = 1, name = main}
async 和serial_queue组合会创建新的线程 但是只会创建一个线程 各个线程做各个的事情 打印就是各打印各的. 异步 穿行只会创建一个线程 在这个所以这个 异步串行是 有顺序的额

// 异步 并行
-(void)asyncConcurrentQueue{
dispatch_queue_t concurrent_queue = dispatch_queue_create("lccc", DISPATCH_QUEUE_CONCURRENT);

for (int i = 0; i<10; i++) {
    dispatch_async(concurrent_queue, ^{
        NSLog(@"i = %d",i);
        NSLog(@"current thread = %@",[NSThread currentThread]);
    });
}
for (int i = 0; i<5; i++) {
    NSLog(@"+++%@",[NSThread currentThread]);
}}

2016-11-07 14:30:55.875 gcd[5596:370161] i = 2
2016-11-07 14:30:55.875 gcd[5596:370159] i = 1
2016-11-07 14:30:55.875 gcd[5596:370127] +++<NSThread: 0x608000076180>{number = 1, name = main}
2016-11-07 14:30:55.875 gcd[5596:370174] i = 0
2016-11-07 14:30:55.876 gcd[5596:370177] i = 5
2016-11-07 14:30:55.876 gcd[5596:370176] i = 4
2016-11-07 14:30:55.875 gcd[5596:370158] i = 3
2016-11-07 14:30:55.876 gcd[5596:370159] current thread = <NSThread: 0x608000263300>{number = 4, name = (null)}
2016-11-07 14:30:55.876 gcd[5596:370127] +++<NSThread: 0x608000076180>{number = 1, name = main}
2016-11-07 14:30:55.876 gcd[5596:370178] i = 6
2016-11-07 14:30:55.876 gcd[5596:370161] current thread = <NSThread: 0x60000007abc0>{number = 3, name = (null)}
2016-11-07 14:30:55.876 gcd[5596:370177] current thread = <NSThread: 0x608000263240>{number = 6, name = (null)}
2016-11-07 14:30:55.876 gcd[5596:370174] current thread = <NSThread: 0x60000007a3c0>{number = 5, name = (null)}
2016-11-07 14:30:55.877 gcd[5596:370179] i = 7
2016-11-07 14:30:55.877 gcd[5596:370158] current thread = <NSThread: 0x608000263380>{number = 8, name = (null)}
2016-11-07 14:30:55.877 gcd[5596:370127] +++<NSThread: 0x608000076180>{number = 1, name = main}
2016-11-07 14:30:55.877 gcd[5596:370180] i = 8
2016-11-07 14:30:55.877 gcd[5596:370159] i = 9
2016-11-07 14:30:55.877 gcd[5596:370176] current thread = <NSThread: 0x608000263280>{number = 7, name = (null)}
2016-11-07 14:30:55.877 gcd[5596:370178] current thread = <NSThread: 0x608000263140>{number = 9, name = (null)}
2016-11-07 14:30:55.877 gcd[5596:370179] current thread = <NSThread: 0x60000007ae80>{number = 10, name = (null)}
2016-11-07 14:30:55.884 gcd[5596:370127] +++<NSThread: 0x608000076180>{number = 1, name = main}
2016-11-07 14:30:55.884 gcd[5596:370180] current thread = <NSThread: 0x6080002631c0>{number = 11, name = (null)}
2016-11-07 14:30:55.884 gcd[5596:370159] current thread = <NSThread: 0x608000263300>{number = 4, name = (null)}
2016-11-07 14:30:55.885 gcd[5596:370127] +++<NSThread: 0x608000076180>{number = 1, name = main}

异步 并行队列分析
貌似和异步 globle队列是一样的....

====================================================================================================
异步的说完了 现在说一下同步
1 同步 mainQueue
2同步 globleQueue
3同步 serialQueue
3同步 concurrentQueue
第一种 我会在下一篇 单独讨论 下面三种简单说一下如图下图


EE9372F8-FA11-490B-A269-DB9481BE1BB6.png

同步的意思是 (不创建线程) 重点哦 当前在神马线程那么block代码块里面就会在 神马线程 并且同步会立马回调 重点哦立马回调
所以 如图所示 for循环所在的线程 是主线程 那么block里面肯定 也是 主线程 下面for 循环也是 主线程 记住同步 会立马回调 并且block里和 下面的for循环 都是zai 一个线程 在任何(一个)线程中做任务 那么都是 有先后顺序的. 所以fou循环 运行的如图 164 行就会 立马回调 打印block里面的 知道fou循环 结束 再 打印下面for循环的 ...

打印效果是这样的


A7F3A549-FAC5-403C-BBCE-45A5BEE83EF7.png

希望我说明白了

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

推荐阅读更多精彩内容