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);

    });

你可能感兴趣的:(dispatch_semaphore_t 信号量 简单控制)