iOS GCD的主线程和子线程

主线程:

    dispatch_async(dispatch_get_main_queue(), ^{

    });

子线程:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 });

子线程(Global Dispatch Queue)有四个优先级:

  • 高优先级(High Priority)
  • 默认优先级(Default Priorty)
  • 低优先级(Low Priority)
  • 后台优先级(Background Priority)

串行队列线程组:

dispatch_queue_t queueT = dispatch_queue_create("group.queue", DISPATCH_QUEUE_CONCURRENT);//一个并发队列
dispatch_group_t grpupT = dispatch_group_create();//一个线程组

dispatch_group_async(grpupT, queueT, ^{
    NSLog(@"group——当前线程一");
    //模仿网络请求代码
    dispatch_group_enter(grpupT);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"网络图片请求中 ···%d", i);
        }
        dispatch_group_leave(grpupT);
    });

});

dispatch_group_async(grpupT, queueT, ^{
    NSLog(@"group——当前线程二");
    //模仿网络请求代码
    dispatch_group_enter(grpupT);

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"网络图片2请求中 ···2_%d", i);
        }
        dispatch_group_leave(grpupT);
    });

});

dispatch_group_async(grpupT, queueT, ^{
    NSLog(@"group——当前线程三");
    //模仿网络请求代码
    dispatch_group_enter(grpupT);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i < 10; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"网络图片3请求中 ···3_%d", i);
        }
         dispatch_group_leave(grpupT);
    });

});
dispatch_group_notify(grpupT, queueT, ^{

    NSLog(@"此时还是在子线程中");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"回到主线程");
    });

});

希望可以帮助大家
如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议
iOS技术交流群:668562416

你可能感兴趣的:(iOS GCD的主线程和子线程)