首页 > 编程语言 > 详细

通过GCD、NSOperationQueue队列、NSThread三种方法来创建多线程

时间:2015-03-29 17:55:19      阅读:179      评论:0      收藏:0      [点我收藏+]
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *remindLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendBtnClick:(UIButton *)sender {

    /*--第一种采用GCD发送请求并刷新UI--*/
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        sleep(2);
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            _remindLabel.text=@"发送成功";
        });
       
    });
     /*--第二种采用NSBlockOperation、NSOperationQueue队列发送请求并刷新UI--*/
     NSOperationQueue *myQueue=[[NSOperationQueue alloc]init];
     NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
       //延时2秒
        sleep(2);
        //主线程刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            
             _remindLabel.text=@"发送成功";
        });
    }];
    [myQueue addOperation:operation];
    
    
    /*--第三种采用NSThread发送请求并刷新UI--*/
    [NSThread detachNewThreadSelector:@selector(sendState) toTarget:self withObject:nil];
    
    
}
-(void)sendState
{
    sleep(2);
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self updateLabel];
    });
}
-(void)updateLabel
{
   _remindLabel.text=@"发送成功";
}
@end

技术分享

通过GCD、NSOperationQueue队列、NSThread三种方法来创建多线程

原文:http://www.cnblogs.com/thbbsky/p/4375961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!