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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值