初级多线程


-(NSString *)fetchSomethingFromServer

{

    [NSThread sleepForTimeInterval:1];

    return @"Hi there";

}

-(NSString *)processData:(NSString *)data

{

    [NSThread sleepForTimeInterval:2];

    return [data uppercaseString];

}

-(NSString *)calculateFirstResult:(NSString *)data

{

    [NSThread sleepForTimeInterval:3];

    return [NSString stringWithFormat:@"Number of chars:%d",[data length]];

}

-(NSString *)calculateSecondResult:(NSString *)data

{

    [NSThread sleepForTimeInterval:4];

    return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];

}

-(void)doWork:(id)sender

{

    NSDate *startTime=[NSDate date];

    __block NSString *resultsSummary=@"a";

    

    self.startButton.enabled=NO;

    self.startButton.alpha=0.5;

    [self.spinner startAnimating];

    

    //设置分派组和队列

    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    dispatch_async(queue, ^{

        

    NSString *fetchedData=[self fetchSomethingFromServer];

    NSString *processedData=[self processData:fetchedData];

        

        __block NSString *firstResult;

        __block NSString *secondResult;

        

    dispatch_group_t group=dispatch_group_create();

    

        dispatch_group_async(group, queue, ^{

        

            firstResult=[self calculateFirstResult:processedData];

        });

        dispatch_group_async(group, queue, ^{

            secondResult=[self calculateSecondResult:processedData];

        });

        

        dispatch_group_notify(group, queue, ^{

            

           resultsSummary=[NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,secondResult];

            NSLog(@"继续运行-->%@",resultsSummary);

        });

        

//    NSString *firstResult=[self calculateFirstResult:processedData];

//    NSString *secondResult=[self calculateSecondResult:processedData];

//    NSString *resultsSummary=[NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,secondResult];

    

    dispatch_async(dispatch_get_main_queue(), ^{

    self.resultsTextView.text=resultsSummary;

        NSLog(@"继续运行-->%@",resultsSummary);

        self.startButton.enabled=YES;

        self.startButton.alpha=1.0;

        [self.spinner stopAnimating];

    });

        

    NSDate *endTime=[NSDate date];

    NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);

    });

}


你可能感兴趣的:(初级多线程)