您的位置:首页 > 其它

10.3 GCD 常用

2015-05-29 16:13 211 查看
//  ViewController.h
//  10 GCD
//
//  Created by Tracy on 15/5/29.
//  Copyright (c) 2015年 Tracy. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
</pre><pre name="code" class="objc">
//
//  ViewController.m
//  10 GCD
//
//  Created by Tracy on 15/5/29.
//  Copyright (c) 2015年 Tracy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;
@property (strong, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)updateProgress:(UIButton *)sender
{
    self.progressView.progress=0;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=0; i<100; i++)
        {

            [NSThread sleepForTimeInterval:0.02f];
            dispatch_async(dispatch_get_main_queue(), ^{
            self.progressView.progress+=0.01;
            });

        }
          });
//    self.progressView.progress=0;
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        for (int i=0; i<100; i++)
//        {
//            
//            [NSThread sleepForTimeInterval:0.02f];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                self.progressView.progress+=0.01;
//            });
//            
//        }
//    });

}
- (IBAction)synDoWork:(UIButton *)sender
{
     //清空textView。此处的清空不起作用,因为线程同步,都执行完了才会回到主线程
    self.textView.text = @"";
    NSMutableString*mStr=[[NSMutableString alloc]init];
    dispatch_queue_t global_queue=dispatch_get_global_queue(0, 0);
    dispatch_sync(global_queue, ^{
        
        [mStr appendString:[self doWork1]];
        
    });
    dispatch_sync(global_queue, ^{
        [mStr appendString:[self doWork2]];
    });
    dispatch_sync(global_queue, ^{
        [mStr appendString:[self doWork2]];
    });
//    OK
    dispatch_async(dispatch_get_main_queue(), ^{
        self.textView.text=mStr;
    });
    
//    以下,主线程按钮与下式 互相等待完成,死锁;
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        self.textView.text=mStr;
//    });

//    OK
//    self.textView.text=mStr;

    
    
}
- (IBAction)aSynDoWork:(UIButton *)sender
{
    self.textView.text=@"";
    NSMutableString*mStr=[[NSMutableString alloc]init];
    
    dispatch_queue_t global_queue=dispatch_get_global_queue(0, 0);
    
    dispatch_async(global_queue, ^{
        [mStr appendString:[self doWork1]];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textView.text=mStr;
        });
        
    });
    
    dispatch_async(global_queue, ^{
        [mStr appendString:[self doWork2]];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textView.text=mStr;
        });

    });
    dispatch_async(global_queue, ^{
        [mStr appendString:[self doWork3]];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textView.text=mStr;
        });

    });
 }
- (IBAction)asynGroupDoWork:(UIButton *)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;
    });

}

-(NSString*)doWork1
{
    [NSThread sleepForTimeInterval:3.0f];
  //  NSLog(@"  1 start");
    return @"  1 finished! \n ";
}

-(NSString*)doWork2
{
    [NSThread sleepForTimeInterval:2.0f];

  //  NSLog(@"  2 start");
    return @"  2 finished! \n ";
}

-(NSString*)doWork3
{
    [NSThread sleepForTimeInterval:1.0f];

  //  NSLog(@"  3 start");
    return @"  3 finished! \n ";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: