iOS开发之用GCD实现多线程

时间:2022-12-01 23:39:00
//因为是异步,所以开通了子线程,但是因为是串行队列,所以只需要开通1个子线程,它们在子线程中顺序执行。最常用。
dispatch_queue_t q1 = dispatch_queue_create("qqqqq", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i < 10; i++) {
   dispatch_async(q1, ^{
       NSLog(@"11111%@",[NSThread currentThread]);
   });
}
//因为是异步,所以开通了子线程,且因为是并行队列,所以开通了好多个子线程,具体几个,无人知晓,看运气。线程数量无法控制,且浪费。
dispatch_queue_t q2 = dispatch_queue_create("wwwww", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 10; i++) {
   dispatch_async(q2, ^{
       NSLog(@"22222%@",[NSThread currentThread]);
   });
}
//因为是同步,所以无论是并行队列还是串行队列,都是在主线程中执行
dispatch_queue_t q3 = dispatch_queue_create("eeeee", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i < 10; i++) {
   dispatch_sync(q3, ^{
       NSLog(@"33333%@",[NSThread currentThread]);
   });
}
//全局队列和并行队列类似(全局队列不需要创建直接get即可,而导致其没有名字,不利于后续调试)
dispatch_queue_t q4 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int i = 0; i < 10; i++) {
   dispatch_sync(q4, ^{
       NSLog(@"44444%@",[NSThread currentThread]);
   });
}
for (int i = 0; i < 10; i++) {
   dispatch_async(q4, ^{
       NSLog(@"55555%@",[NSThread currentThread]);
   });
}

打印结果

2016-06-30 23:10:54.486 multithreaded[10087:519523] 22222<NSThread: 0x7f9a7ccd5470>{number = 5, name = (null)}

2016-06-30 23:10:54.486 multithreaded[10087:519524] 22222<NSThread: 0x7f9a7cf1a250>{number = 6, name = (null)}

2016-06-30 23:10:54.486 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.486 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.486 multithreaded[10087:519525] 22222<NSThread: 0x7f9a7cf3d720>{number = 7, name = (null)}

2016-06-30 23:10:54.486 multithreaded[10087:519518] 22222<NSThread: 0x7f9a7ce151a0>{number = 3, name = (null)}

2016-06-30 23:10:54.486 multithreaded[10087:519522] 22222<NSThread: 0x7f9a7cd1cf20>{number = 4, name = (null)}

2016-06-30 23:10:54.487 multithreaded[10087:519526] 22222<NSThread: 0x7f9a7ccd8e80>{number = 8, name = (null)}

2016-06-30 23:10:54.487 multithreaded[10087:519523] 22222<NSThread: 0x7f9a7ccd5470>{number = 5, name = (null)}

2016-06-30 23:10:54.487 multithreaded[10087:519524] 22222<NSThread: 0x7f9a7cf1a250>{number = 6, name = (null)}

2016-06-30 23:10:54.487 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.487 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.488 multithreaded[10087:519527] 22222<NSThread: 0x7f9a7ce0d8e0>{number = 9, name = (null)}

2016-06-30 23:10:54.488 multithreaded[10087:519525] 22222<NSThread: 0x7f9a7cf3d720>{number = 7, name = (null)}

2016-06-30 23:10:54.489 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.489 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.541 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.541 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.541 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.541 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.541 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.541 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.541 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.541 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.541 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.542 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.542 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.542 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.542 multithreaded[10087:519432] 33333<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.542 multithreaded[10087:519517] 11111<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.542 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.542 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.543 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.543 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.543 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519432] 44444<NSThread: 0x7f9a7ce044b0>{number = 1, name = main}

2016-06-30 23:10:54.544 multithreaded[10087:519517] 55555<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.544 multithreaded[10087:519525] 55555<NSThread: 0x7f9a7cf3d720>{number = 7, name = (null)}

2016-06-30 23:10:54.544 multithreaded[10087:519527] 55555<NSThread: 0x7f9a7ce0d8e0>{number = 9, name = (null)}

2016-06-30 23:10:54.544 multithreaded[10087:519523] 55555<NSThread: 0x7f9a7ccd5470>{number = 5, name = (null)}

2016-06-30 23:10:54.544 multithreaded[10087:519524] 55555<NSThread: 0x7f9a7cf1a250>{number = 6, name = (null)}

2016-06-30 23:10:54.544 multithreaded[10087:519526] 55555<NSThread: 0x7f9a7ccd8e80>{number = 8, name = (null)}

2016-06-30 23:10:54.545 multithreaded[10087:519522] 55555<NSThread: 0x7f9a7cd1cf20>{number = 4, name = (null)}

2016-06-30 23:10:54.545 multithreaded[10087:519518] 55555<NSThread: 0x7f9a7ce151a0>{number = 3, name = (null)}

2016-06-30 23:10:54.545 multithreaded[10087:519517] 55555<NSThread: 0x7f9a7cc13150>{number = 2, name = (null)}

2016-06-30 23:10:54.545 multithreaded[10087:519525] 55555<NSThread: 0x7f9a7cf3d720>{number = 7, name = (null)}

//GCD的一些高级用法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
    NSLog(@"10秒后执行");//这个是真正的sleep10秒后执行
});

//让后台2个线程并行执行,然后等2个线程都结束后,再汇总执行结果。
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
    // 并行执行的线程一
    for (int i = 0; i < 100; i++) {
        NSLog(@"1111");
    }
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
    // 并行执行的线程二
    for (int i = 0; i < 100; i++) {
        NSLog(@"2222");
    }
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
    // 汇总结果
    for (int i = 0; i < 100; i++) {
        NSLog(@"3333");
    }
});