iOS中多线程的实现方式及对比

iOS中多线程实现的方式有三种,分别为NSThread、GCD、NSOperation
对比:
NSThread 优点:NSThread相对于GCD、NSOperation更加轻量级,使用相对简单;
 缺点:NSThread需要自己管理多线程的生命周期、线程同步、加锁、睡眠以及唤醒等
NSOperation 面向对象的多线程;

GCD是一种多核编程的解决方案,用于替代NSThread,iOS4.0以上才能使用

NSThread的实现方式:

- (void)loadView

{

    UIView *backgroundView = [[UIView allocinitWithFrame:CGRectMake(0.0f0.0fScreen_WidthScreen_Height)];

    

    UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    downloadBtn.frame = CGRectMake(20.0fStatusBar_Height + 80.0f200.0f40.0f);

    downloadBtn.backgroundColor = OrangeColor;

    [downloadBtn setTitle:@"下载"

                 forState:UIControlStateNormal];

    [downloadBtn setTitle:@"下载"

                 forState:UIControlStateSelected];

    [downloadBtn addTarget:self

                    action:@selector(download:)

          forControlEvents:UIControlEventTouchUpInside];

    [backgroundView addSubview:downloadBtn];

    

    self.imageView = [[UIImageView allocinitWithFrame:CGRectMake(20.0f140.0f280.0f300.0f)];

    [backgroundView addSubview:self.imageView];

    

    self.view = backgroundView;

}


- (void)download:(UIButton*)sender

{

    NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];

    

    NSThread *thread = [[NSThread allocinitWithTarget:self

                                               selector:@selector(downloadImage:)

                                                 object:url];

    [thread start];

}


- (void)downloadImage:(NSURL*)url

{

    NSData *data = [[NSData allocinitWithContentsOfURL:url];

    if (data) {

        UIImage *image = [[UIImage allocinitWithData:data];

        [self performSelectorOnMainThread:@selector(updateUI:)

                               withObject:image

                            waitUntilDone:YES];

    }

}


- (void)updateUI:(UIImage*)image

{

    self.imageView.image = image;

}


NSOperation的实现方式:



- (void)download:(UIButton*)sender

{

    NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];

    NSInvocationOperation *operation = [[NSInvocationOperation allocinitWithTarget:self

                                                                            selector:@selector(downloadImage:)

                                                                              object:url];

    NSOperationQueue *queue = [[NSOperationQueue allocinit];

    [queue addOperation:operation];

}


- (void)downloadImage:(NSURL*)url

{

    NSData *data = [[NSData allocinitWithContentsOfURL:url];

    if (data) {

        UIImage *image = [[UIImage allocinitWithData:data];

        [self performSelectorOnMainThread:@selector(updateUI:)

                               withObject:image

                            waitUntilDone:YES];

    }

}


- (void)updateUI:(UIImage*)image

{

    self.imageView.image = image;

}


GCD的实现方式:


- (void)download:(UIButton*)sender

{

    //异步更新

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT0), ^{

        NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];

        NSData *data = [[NSData allocinitWithContentsOfURL:url];

        if (data) {

            UIImage *image = [[UIImage allocinitWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^{

                self.imageView.image = image;

            });

        }

    });

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

//    dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {

//        

//    });

    

    //dispatch_group_t 分组,将所有分组中的任务执行后,再更新界面

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT0);

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group, queue, ^{

        NSLog(@"任务一");

    });

    dispatch_group_async(group, queue, ^{

        NSLog(@"任务二");

    });

    dispatch_group_async(group, queue, ^{

        NSLog(@"任务三");

    });

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{

       //监测三组任务是否完全完成,完成后更新界面

        

    });

    

    //dispatch_barrier_async 表示前面的任务执行后,再执行dispatch_barrier_async 任务

    dispatch_queue_t queue1 = dispatch_queue_create("com.liusanchun.gcd"DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue1, ^{

        NSLog(@"任务一");

    });

    dispatch_async(queue1, ^{

        NSLog(@"任务二");

    });

    dispatch_barrier_async(queue1, ^{

        NSLog(@"执行");

    });

    dispatch_async(queue1, ^{

        NSLog(@"任务三");

    });

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值