ios GCD之同步异步线程、线程组

  1. 最常用GCD方法
dispatch_async(dispatch_get_global_queue(0,0),^{

NSLog(@"Start task 1");

[NSThread sleepForTimeInterval:3];

dispatch_async(dispatch_get_main_queue(),^{

//回到主线程刷新UI

NSLog(@"刷新UI");

});

});
  1. 系统分配子线程应用
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       NSLog(@"start task 1");

       [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 1");

    });

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

       NSLog(@"start task 2");

        [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 2");

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

        NSLog(@"start task 3");

       [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 3");

   });
  1. 自行创建子线程
 //NULL == DISPATCH_QUEUE_SERIAL (同步线程)

    // DISPATCH_QUEUE_CONCURRENT  (异步线程)

   dispatch_queue_t queue = dispatch_queue_create("com.test.gcg.queue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{

        NSLog(@"start task 1");

        [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 1");

    });

     dispatch_async(queue, ^{

        NSLog(@"start task 2");

        [NSThread sleepForTimeInterval:2];

       NSLog(@"end task 2");

   });

    dispatch_async(queue, ^{

       NSLog(@"start task 3");

        [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 3");

  });
  1. 线程组
 dispatch_queue_t queue1 = dispatch_queue_create("com.test.gcg.group", DISPATCH_QUEUE_CONCURRENT);

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group, queue1, ^{

       NSLog(@"start task 1");

        [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 1");

    });
    dispatch_group_async(group, queue1, ^{

        NSLog(@"start task 2");

        [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 2");

    });
    dispatch_group_async(group, queue1, ^{

        NSLog(@"start task 3");

       [NSThread sleepForTimeInterval:2];

        NSLog(@"end task 3");

    });
  1. 线程组高级应用,很常见的情形:开辟两个网络请求,都结束之后,执行下一步操作。

(1)比较常规的做法是设置标示符,两线程结束后分别修改状态,两个标识符都改变后,执行下一步操作。

(2)运行线程组的:

dispatch_group_notify;

dispatch_group_enter(group);

dispatch_group_leave(group);

组合能完美解决此问题:

 dispatch_group_enter(group);

    [self sendRequest1:^{

        NSLog(@"request1 done");

        dispatch_group_leave(group);

    }];

    dispatch_group_enter(group);

    [self sendRequest1:^{

        NSLog(@"request2 done");

        dispatch_group_leave(group);

    }];

  dispatch_group_notify(group, queue1, ^{

   NSLog(@"All tasks over");

   dispatch_async(dispatch_get_main_queue(), ^{

   NSLog(@"回到主线程刷新UI");

});

});

-(void)sendRequest1:(void(^)())block{

dispatch_async(dispatch_get_global_queue(0,0),^{

NSLog(@"start task 1");

[NSThreadsleepForTimeInterval:2];

NSLog(@"end task 1");

dispatch_async(dispatch_get_main_queue(),^{

if(block){

block();

}
});

});

}
-(void)sendRequest2:(void(^)())block{

dispatch_async(dispatch_get_global_queue(0,0),^{

NSLog(@"start task 2");

[NSThreadsleepForTimeInterval:2];

NSLog(@"end task 2");

dispatch_async(dispatch_get_main_queue(),^{

if(block){

block();

}
});

});
}

你可能感兴趣的:(ios GCD之同步异步线程、线程组)