GCD死锁,及同步、异步、串行和并行队列组合情形
dispatch_queue_t queue2 = dispatch_get_global_queue(0, 0); dispatch_async(queue2, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 10:33:28.408678+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.408685+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.408935+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.408935+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.409076+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.409098+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.409185+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.409232+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.409301+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.409618+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.409873+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.410141+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.410480+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.410794+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-14 10:33:28.411167+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.411486+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 2019-02-2019-02-14 10:33:28.411962+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)} 14 10:33:28.411763+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.422231+0800 MJ_iOS_Test[1864:38570] 执行任务1-----<NSThread: 0x600000c16980>{number = 1, name = main} 2019-02-14 10:33:28.422250+0800 MJ_iOS_Test[1864:38623] 执行任务3-----<NSThread: 0x600000c7b780>{number = 3, name = (null)}
dispatch_queue_t queue1 = dispatch_get_main_queue(); dispatch_sync(queue1, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } });
//错误
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL);
NSLog(@"执行任务1----%@", [NSThread currentThread]);
dispatch_async(queue3, ^{
NSLog(@"执行任务2----%@", [NSThread currentThread]);
dispatch_sync(queue3, ^{
NSLog(@"执行任务3----%@", [NSThread currentThread]);
});
NSLog(@"执行任务4----%@", [NSThread currentThread]);
});
NSLog(@"执行任务5----%@", [NSThread currentThread]);
//打印
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL); dispatch_queue_t queue4 = dispatch_queue_create("myQueue4", DISPATCH_QUEUE_SERIAL); NSLog(@"执行任务1----%@", [NSThread currentThread]); dispatch_async(queue3, ^{ NSLog(@"执行任务2----%@", [NSThread currentThread]); dispatch_sync(queue4, ^{ NSLog(@"执行任务3----%@", [NSThread currentThread]); }); NSLog(@"执行任务4----%@", [NSThread currentThread]); }); NSLog(@"执行任务5----%@", [NSThread currentThread]);
//打印
2019-02-15 12:28:51.782328+0800 MJ_iOS_Test[3879:128281] 执行任务1----<NSThread: 0x600003661480>{number = 1, name = main} 2019-02-15 12:28:51.782639+0800 MJ_iOS_Test[3879:128281] 执行任务5----<NSThread: 0x600003661480>{number = 1, name = main} 2019-02-15 12:28:51.782678+0800 MJ_iOS_Test[3879:128333] 执行任务2----<NSThread: 0x600003607fc0>{number = 3, name = (null)} 2019-02-15 12:28:51.782818+0800 MJ_iOS_Test[3879:128333] 执行任务3----<NSThread: 0x600003607fc0>{number = 3, name = (null)} 2019-02-15 12:28:51.782955+0800 MJ_iOS_Test[3879:128333] 执行任务4----<NSThread: 0x600003607fc0>{number = 3, name = (null)}
//分析
<1>dispatch_sync本身运行在子线程中,而该子线程对应的队列为queue3,因此A2(即添加任务本身)存放在queue3中,而3存放在queue4中;
<2>当子线程运行完毕2后(并随即自动销毁),此时系统是从queue3中取出4还是从queue4取出3放到子线程中运行呢?——因为同步,系统不会开辟新的线程,添加的block任务依然运行在当前线程中(number为3的子线程);同时,A2会一直停留在queue3中,直到3执行完毕才会自动销毁,又因为queue3是个串行队列而4排在A2后面,所以A2把4给堵住了,当3运行完毕后,A2销毁,系统就从queue3中取出4放到当前线程中运行;
注:此处将3放到一个并发队列中,效果是一样的;
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL); NSLog(@"执行任务1----%@", [NSThread currentThread]); dispatch_async(queue3, ^{ NSLog(@"执行任务2----%@", [NSThread currentThread]); dispatch_async(queue3, ^{ NSLog(@"执行任务3----%@", [NSThread currentThread]); }); NSLog(@"执行任务4----%@", [NSThread currentThread]); }); NSLog(@"执行任务5----%@", [NSThread currentThread]);
//打印
2019-02-15 14:09:46.953030+0800 MJ_iOS_Test[5523:191263] 执行任务1----<NSThread: 0x6000030aa900>{number = 1, name = main} 2019-02-15 14:09:46.953330+0800 MJ_iOS_Test[5523:191263] 执行任务5----<NSThread: 0x6000030aa900>{number = 1, name = main} 2019-02-15 14:09:46.953326+0800 MJ_iOS_Test[5523:191330] 执行任务2----<NSThread: 0x6000030c2880>{number = 3, name = (null)} 2019-02-15 14:09:46.953608+0800 MJ_iOS_Test[5523:191330] 执行任务4----<NSThread: 0x6000030c2880>{number = 3, name = (null)} 2019-02-15 14:09:46.954050+0800 MJ_iOS_Test[5523:191330] 执行任务3----<NSThread: 0x6000030c2880>{number = 3, name = (null)}
//分析——我门只分析3和4的顺序
<1>因为是异步且queue3非主队列,所以系统理应开辟一个新线程,但是运行完2后,此刻当前线程(number为3的子线程)处于闲置状态,为了节省资源,系统并不会真的开辟一个新线程,所有后面的任务依然会运行在当前子线程中(除非当前线程正在运行中,而同时要异步运行另一个任务,系统才会考虑开启一个新线程,或者手动强行开启一个新线程(异步并发));
<2>首先,我们明确下A2(第二个dispatch_async添加任务本身)、3和4在queue3中的存放顺序:
因为3的存放是由A2在线程上运行完成的,而在编译阶段A2并不会运行,但此时系统会给A2和4分配位置(依据代码顺序)——因此A2和4的存放是在编译阶段完成的,而3的存放是在运行阶段完成的,所以如下图所示
<3>因为异步,所以A2不会停留在queue3中一直等待3执行完毕而是将3添加到queue3中随即自动销毁,因为queue3是个串行队列,此时系统就从queue3中取出4放到当前子线程中运行,待4运行完毕,系统又会取出3放到当前子线程中运行;
说明:dispatch添加block任务(不论异步还是同步),都是在当前线程上运行完成的,因此该block任务的存放位置一定在dispatch后面所有外部任务(后面dispatch中的block任务以此类推)的位置后面———这点,请注意!!!
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL); dispatch_sync(queue3, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 11:10:57.529267+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.529503+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.529627+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.529771+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.529903+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530018+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530147+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530270+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530418+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530547+0800 MJ_iOS_Test[2440:59133] 执行任务3-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530666+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.530898+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.531152+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.549535+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.549711+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.549853+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.549989+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.550113+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.550247+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main} 2019-02-14 11:10:57.550381+0800 MJ_iOS_Test[2440:59133] 执行任务1-----<NSThread: 0x60000252ce40>{number = 1, name = main}
dispatch_queue_t queue2 = dispatch_get_global_queue(0, 0); for (int i = 0; i < 5; i++) { NSLog(@"执行任务1----%@", [NSThread currentThread]); } dispatch_async(queue2, ^{ for (int i = 0; i < 5; i++) { NSLog(@"执行任务2----%@", [NSThread currentThread]); } dispatch_sync(queue2, ^{ for (int i = 0; i < 5; i++) { NSLog(@"执行任务3----%@", [NSThread currentThread]); } }); for (int i = 0; i < 5; i++) { NSLog(@"执行任务4----%@", [NSThread currentThread]); } }); for (int i = 0; i < 5; i++) { NSLog(@"执行任务5----%@", [NSThread currentThread]); }
//打印
2019-02-15 17:19:50.417681+0800 MJ_iOS_Test[8640:311746] 执行任务1----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.417926+0800 MJ_iOS_Test[8640:311746] 执行任务1----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418055+0800 MJ_iOS_Test[8640:311746] 执行任务1----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418175+0800 MJ_iOS_Test[8640:311746] 执行任务1----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418292+0800 MJ_iOS_Test[8640:311746] 执行任务1----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418449+0800 MJ_iOS_Test[8640:311746] 执行任务5----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418492+0800 MJ_iOS_Test[8640:311806] 执行任务2----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.418569+0800 MJ_iOS_Test[8640:311746] 执行任务5----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.418727+0800 MJ_iOS_Test[8640:311806] 执行任务2----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.419157+0800 MJ_iOS_Test[8640:311746] 执行任务5----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.419427+0800 MJ_iOS_Test[8640:311806] 执行任务2----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.419705+0800 MJ_iOS_Test[8640:311746] 执行任务5----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.419987+0800 MJ_iOS_Test[8640:311806] 执行任务2----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.420301+0800 MJ_iOS_Test[8640:311746] 执行任务5----<NSThread: 0x600002e96900>{number = 1, name = main} 2019-02-15 17:19:50.427851+0800 MJ_iOS_Test[8640:311806] 执行任务2----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.428027+0800 MJ_iOS_Test[8640:311806] 执行任务3----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.428232+0800 MJ_iOS_Test[8640:311806] 执行任务3----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.429049+0800 MJ_iOS_Test[8640:311806] 执行任务3----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.429279+0800 MJ_iOS_Test[8640:311806] 执行任务3----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.429431+0800 MJ_iOS_Test[8640:311806] 执行任务3----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.429774+0800 MJ_iOS_Test[8640:311806] 执行任务4----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.430498+0800 MJ_iOS_Test[8640:311806] 执行任务4----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.430812+0800 MJ_iOS_Test[8640:311806] 执行任务4----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.431099+0800 MJ_iOS_Test[8640:311806] 执行任务4----<NSThread: 0x600002ef8680>{number = 3, name = (null)} 2019-02-15 17:19:50.431531+0800 MJ_iOS_Test[8640:311806] 执行任务4----<NSThread: 0x600002ef8680>{number = 3, name = (null)}
//分析
<1>当主线程(以下简称“T0”)运行完1后,程序走到第一个异步并发,此时系统必然开启一个新线程(以下简称“T1”),该线程运行第一个大block中的内容(以下简称“B1”),而主线程继续运行5,5和第一个大block中的内容的运行互不影响,交替执行,这点没问题;
<2>但是,问题来了—————第一个大block中的内容要先执行哪一个呢?为什么是2跟5交替执行,而其他都是顺序执行呢?
$0:首先要明确一点,不论是串行队列还是并行队列,其内部任务的添加都要遵循先来后到的顺序;
$1:各个队列中任务的位置顺序:主队列中存放三个任务,顺序依次为1、第一个async任务本身(以下简称“A1”)和5,均由主线程来运行并且A1不会等待,所以1和A1执行完毕才能执行5,这点没问题;queue2中存放2、第二个sync任务本身(以下简称“A2”)、3和4四个任务,按照上述分析,dispatch添加的block任务3会在2和4之后,所以存放顺序依次为2、A2、4和3,如下图;
$2:当程序进入B1后A2之前,此时的所有任务都只会运行在当前线程T1中,走到A2时,因为是同步,所以不会开启新线程,因此B1始终运行在T1上;而并发队列只有在异步时才有效,即如果只有一条线程时则只能顺序执行;
$3:因此,T1会先运行2,此时T0在运行5,两条线程互不影响,交替运行;待2运行完后,按顺序系统会从queue2中取出A2放到T1中运行,因为是同步,A2把3添加到queue2队列中后依然停留在queue2中等待3运行完毕而不会立即销毁;
----问题:此时queue2不会死锁吗?系统是从queue2中先取出3还是先取出4放到T1上运行呢?
第一个问题:因为只有一条线程,所以queue2虽为并行队列,但也只能按顺序运行其内部任务,但因为是并行队列,系统是可以从queue2中取出任何一个任务放到线程上运行,场景理解:
死锁:串行队列好比一根独木桥(横向距离刚好只能站住一个人,桥下是悬崖),桥上有ABCDE五个人依次排队进火车站检票入口(有且仅有一个);
同步并行:上述五个人站在大马路上(马路很宽)排队进检票口;
系统:检票员;
因此:如果是死锁,检票员无法跑到D身边让D先进检票口——否则会掉进悬崖;而同步并行就可以;
所以,串行队列,系统要按顺序取队列中的任务;并行,系统可以随机取————这点,请注意!!!;
第二个问题:因为A2一直在等待3运行完毕,即告诉系统要立马将3取出运行;
所以,同步即优先(立马)运行其添加的block任务————这点,请注意!!!
dispatch_queue_t queue1 = dispatch_get_main_queue(); dispatch_async(queue1, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 15:18:24.484445+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.484665+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.484837+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.484978+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485116+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485296+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485430+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485566+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485698+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.485815+0800 MJ_iOS_Test[6008:173463] 执行任务1-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.507778+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.507968+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.508119+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.508252+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.508388+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.508541+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.508695+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.509382+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.509973+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main} 2019-02-14 15:18:24.510909+0800 MJ_iOS_Test[6008:173463] 执行任务3-----<NSThread: 0x600002642940>{number = 1, name = main}
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL); dispatch_async(queue3, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 15:35:12.843681+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.843686+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.843933+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.843999+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.844065+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.844208+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.844348+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.844474+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.844482+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.844614+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.844743+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.844769+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.845040+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.845234+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.845559+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.854636+0800 MJ_iOS_Test[6248:181935] 执行任务3-----<NSThread: 0x6000011c9e00>{number = 3, name = (null)} 2019-02-14 15:35:12.855004+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.855578+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.855858+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main} 2019-02-14 15:35:12.856068+0800 MJ_iOS_Test[6248:181875] 执行任务1-----<NSThread: 0x6000011aae40>{number = 1, name = main}
dispatch_queue_t queue3 = dispatch_queue_create("myQueue3", DISPATCH_QUEUE_SERIAL); dispatch_async(queue3, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); dispatch_async(queue3, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-2-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 15:48:52.982315+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.982310+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.982607+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.982607+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.982759+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.982759+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.982871+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.982917+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.982970+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.983061+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.983401+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.983668+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.983945+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.984186+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.984453+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.984710+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.984928+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.985130+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.994169+0800 MJ_iOS_Test[6484:189759] 执行任务1-----<NSThread: 0x600000356840>{number = 1, name = main} 2019-02-14 15:48:52.994181+0800 MJ_iOS_Test[6484:189815] 执行任务3-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.994355+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.994502+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.994671+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.994804+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.995173+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.995663+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.996280+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.996518+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.996819+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)} 2019-02-14 15:48:52.997497+0800 MJ_iOS_Test[6484:189815] 执行任务3-2-----<NSThread: 0x600000320940>{number = 3, name = (null)}
dispatch_queue_t queue2 = dispatch_get_global_queue(0, 0); dispatch_async(queue2, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-----%@", [NSThread currentThread]); } }); dispatch_async(queue2, ^{ for (int i = 0; i < 10; i++) { NSLog(@"执行任务3-2-----%@", [NSThread currentThread]); } }); for (int i = 0; i < 10; i++) { NSLog(@"执行任务1-----%@", [NSThread currentThread]); }
//打印
2019-02-14 16:06:55.818176+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.818227+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.818227+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.818495+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.818500+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.818500+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.818630+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.818657+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.818753+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.818758+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.819027+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.819361+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.819644+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.820062+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.820389+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.820744+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.821125+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.821634+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = 2019-02-14 16:06:55.822389+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} (null)} 2019-02-14 16:06:55.821989+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.833276+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.833308+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.833282+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.833419+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.833462+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.833503+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.833543+0800 MJ_iOS_Test[6737:199038] 执行任务1-----<NSThread: 0x600002ed1180>{number = 1, name = main} 2019-02-14 16:06:55.833890+0800 MJ_iOS_Test[6737:199118] 执行任务3-----<NSThread: 0x600002eb2380>{number = 3, name = (null)} 2019-02-14 16:06:55.834534+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)} 2019-02-14 16:06:55.835481+0800 MJ_iOS_Test[6737:199117] 执行任务3-2-----<NSThread: 0x600002eb23c0>{number = 4, name = (null)}