dispatch_semaphore_t 信号量 简单控制

NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageUrlPList" ofType:@"plist"];

    NSDictionary *imageDic = [[NSDictionary alloc] initWithContentsOfFile:path];


    NSString*image1 = [imageDicvalueForKey:@"image1"];

    image1 = [image1stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSString*image2 = [imageDicvalueForKey:@"image2"];

    image2 = [image2stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSString*image3 = [imageDicvalueForKey:@"image3"];

    image3 = [image3stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    dispatch_queue_t queue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_group_t group = dispatch_group_create();

    NSLog(@"Start%@", [NSThread currentThread]);


    dispatch_group_async(group, queue, ^{


        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        dispatch_group_enter(group);


        dispatch_async(queue, ^{

            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:image1]];

            if(data) {

                [self.arrayaddObject:data];

            }

            dispatch_semaphore_signal(sema);

            NSLog(@"1111111%@", [NSThread currentThread]);

        });

        dispatch_async(queue, ^{

            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:image2]];

            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

            if(data) {

                [self.arrayaddObject:data];

            }

            NSLog(@"2222222%@", [NSThread currentThread]);

            dispatch_group_leave(group);

        });

        NSLog(@"Group1111111%@", [NSThread currentThread]);

    });



    dispatch_group_async(group, queue, ^{

        dispatch_group_enter(group);

        dispatch_async(queue, ^{

            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:image3]];

            if(data) {

                [self.arrayaddObject:data];

            }

            NSLog(@"333333333%@", [NSThread currentThread]);

            dispatch_group_leave(group);

        });

        NSLog(@"Group22222222%@", [NSThread currentThread]);

    });



    dispatch_group_notify(group, dispatch_get_main_queue(), ^{

        NSLog(@"AllFinish%@-%@", [NSThread currentThread], self.array);

    });

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容