ios GCD

//
//  MainViewController.m
//  GCD
//
//  Created by hejin on 13-12-29.
//  Copyright (c) 2013年 e世雕龙. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()

@property (strong, nonatomic) IBOutlet UIProgressView *myProgress;//进度条
@property (strong, nonatomic) IBOutlet UITextView *textView;//多行文本框

@end

@implementation MainViewController
//GCD方式更新进度条
- (IBAction)updateProgress:(id)sender {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=0; i<100; i++) {
            [NSThread sleepForTimeInterval:0.02];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.myProgress.progress += 0.01;
            });
        }
    });
}
//GCD并发多线程完成任务
- (IBAction)asynDowork:(id)sender {
    self.textView.text = @"";
    NSMutableString *strWork = [NSMutableString stringWithCapacity:20];
    //不使用多线程
    /*[strWork appendFormat:@"%@\n",[self doWork1]];
    [strWork appendFormat:@"%@\n",[self doWork2]];
    [strWork appendFormat:@"%@\n",[self doWork3]];
    self.textView.text = strWork;*/
    //获取已经存在并始终可用的全局队列
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    //获取主线程队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    //并发多线程完成任务
    dispatch_async(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork1]];
        NSLog(@"doWork1");
        dispatch_async(mainQueue, ^{
            self.textView.text = strWork;
            NSLog(@"updateUI");
        });
    });
    dispatch_async(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork2]];
        NSLog(@"doWork2");
        dispatch_async(mainQueue, ^{
            self.textView.text = strWork;
            NSLog(@"updateUI");
        });
    });
    dispatch_async(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork3]];
        NSLog(@"doWork3");
        dispatch_async(mainQueue, ^{
            self.textView.text = strWork;
            NSLog(@"updateUI");
        });
    });
}
//GCD同步完成任务
- (IBAction)synDowork:(id)sender {
    self.textView.text = @"";
    NSMutableString *strWork = [NSMutableString stringWithCapacity:20];
    //获取已经存在并始终可用的全局队列
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    //获取主线程队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    //同步完成任务
    dispatch_sync(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork1]];
        NSLog(@"doWork1");
    });
    dispatch_sync(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork2]];
        NSLog(@"doWork2");
    });
    dispatch_sync(globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork3]];
        NSLog(@"doWork3");
    });
    //在主线程中更新UI,如下代码同步等待主线程中按钮点击事件完成才进行,主线程中按钮点击事件等待如下代码完成,造成阻塞
    /*dispatch_sync(globalQueue, ^{
        dispatch_sync(mainQueue, ^{
            self.textView.text = strWork;
        });
    });*/
    dispatch_async(globalQueue, ^{
        dispatch_async(mainQueue, ^{
            self.textView.text = strWork;
        });
    });
}
//使用GCD推荐的分组派发同步完成任务
- (IBAction)groupSynDowork:(id)sender {
    self.textView.text = @"";
    NSMutableString *strWork = [NSMutableString stringWithCapacity:20];
    //创建组和队列
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    //队列在组中执行操作
    dispatch_group_async(group, globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork1]];
        NSLog(@"doWork1");
    });
    dispatch_group_async(group, globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork2]];
        NSLog(@"doWork2");
    });
    dispatch_group_async(group, globalQueue, ^{
        [strWork appendFormat:@"%@\n",[self doWork3]];
        NSLog(@"doWork3");
    });
    //通知组中任务完成在主线程中更新UI
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        self.textView.text = strWork;
    });
}
//任务1
- (NSString *)doWork1 {
    [NSThread sleepForTimeInterval:2];
    return @"任务1完成!";
}
//任务2
- (NSString *)doWork2 {
    [NSThread sleepForTimeInterval:3];
    return @"任务2完成!";
}
//任务3
- (NSString *)doWork3 {
    [NSThread sleepForTimeInterval:4];
    return @"任务3完成!";
}
@end

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。