//
//? ViewController.m
//? 信號(hào)量
//
//? Created by Ben Lv on 2018/9/3.
//? Copyright ? 2018年 avatar. All rights reserved.
//
#import "ViewController.h"
#import "ATBaseRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
//? ? [self text];
? ? [selftext2];
//? ? [self text3];
}
// 兩種寫法一樣(text 和 text111),都是異步請(qǐng)求,不保證順序
- (void)text{
? ? //? ? /創(chuàng)建信號(hào)量為0
? ? dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
? ? dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
? ? dispatch_group_t group = dispatch_group_create();
? ? dispatch_group_async(group, queue, ^{
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/journalismApi" param:nil success:^(id data) {
//? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"1請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
//? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"1請(qǐng)求失敗");
? ? ? ? }];
? ? });
? ? dispatch_group_async(group, queue, ^{
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinApi?type=1&page=1" param:nil success:^(id data) {
? ? ? ? ? ? //? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"2請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? ? ? //? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"2請(qǐng)求失敗");
? ? ? ? }];
? ? });
? ? dispatch_group_notify(group, queue, ^{
//? ? ? ? 信號(hào)量 -1 為0時(shí)wait會(huì)阻塞線程
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? NSLog(@"信號(hào)量為0");
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinCommentApi?id=27610708&page=1" param:nil success:^(id data) {
? ? ? ? ? ? NSLog(@"3請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? }];
? ? });
}
- (void)text111 {
? ? //? ? /創(chuàng)建信號(hào)量為0
? ? dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
? ? dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
? ? dispatch_group_t group = dispatch_group_create();
? ? dispatch_group_async(group, queue, ^{
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/journalismApi" param:nil success:^(id data) {
//? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"1請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
//? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"1請(qǐng)求失敗");
? ? ? ? }];
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinApi?type=1&page=1" param:nil success:^(id data) {
? ? ? ? ? ? //? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"2請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? ? ? //? ? ? ? ? ? 信號(hào)量??+1
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"2請(qǐng)求失敗");
? ? ? ? }];
? ? });
//? ? dispatch_group_async(group, queue, ^{
//
//
//
//? ? });
? ? dispatch_group_notify(group, queue, ^{
//? ? ? ? 信號(hào)量 -1 為0時(shí)wait會(huì)阻塞線程
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? NSLog(@"信號(hào)量為0");
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinCommentApi?id=27610708&page=1" param:nil success:^(id data) {
? ? ? ? ? ? NSLog(@"3請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? }];
? ? });
}
//主線程阻塞
- (void)text2{
? ? NSLog(@"current1:%@",[NSThread currentThread]);
? ? dispatch_semaphore_t? semaphore =dispatch_semaphore_create(0);
? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinApi?type=1&page=1" param:nil success:^(id data) {
? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? NSLog(@"2請(qǐng)求成功");
? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? dispatch_semaphore_signal(semaphore);
? ? }];
? ? NSLog(@"你會(huì)來這兒?jiǎn)?");
? ? NSLog(@"current1:%@",[NSThread currentThread]);
? ? //? ? #######阻塞了主線程
? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); //等待信號(hào),當(dāng)信號(hào)總量少于0 的時(shí)候就會(huì)一直等待 ,否則就可以正常的執(zhí)行,并讓信號(hào)總量-1
? ? NSLog(@"你會(huì)來這兒?jiǎn)?");
? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinCommentApi?id=27610708&page=1" param:nil success:^(id data) {
? ? ? ? NSLog(@"3請(qǐng)求成功");
? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? }];
}
- (void)text3{
? ? dispatch_queue_t queue = dispatch_queue_create("ben", NULL);
? ? dispatch_async(queue, ^{
?? ? ? ? NSLog(@"current1:%@",[NSThread currentThread]);
? ? ? ? dispatch_semaphore_t? semaphore =dispatch_semaphore_create(0);
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/journalismApi" param:nil success:^(id data) {
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"1請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? }];
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); //等待信號(hào),當(dāng)信號(hào)總量少于0 的時(shí)候就會(huì)一直等待 ,否則就可以正常的執(zhí)行,并讓信號(hào)總量-1
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinApi?type=1&page=1" param:nil success:^(id data) {
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? NSLog(@"2請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? }];
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); //等待信號(hào),當(dāng)信號(hào)總量少于0 的時(shí)候就會(huì)一直等待 ,否則就可以正常的執(zhí)行,并讓信號(hào)總量-1
? ? ? ? [ATBaseRequest getRequestWithMethod:@"https://www.apiopen.top/satinCommentApi?id=27610708&page=1" param:nil success:^(id data) {
? ? ? ? ? ? NSLog(@"3請(qǐng)求成功");
? ? ? ? }failure:^(NSIntegerresultId,NSString*errorMsg) {
? ? ? ? }];
? ? });
}
@end