//

//? 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容