美文网首页
iOS开发多线程篇-NSThread

iOS开发多线程篇-NSThread

作者: 码代码的小马 | 来源:发表于2017-03-22 09:58 被阅读35次

    上篇我们学习了iOS多线程解决方式中的NSOperation,这篇我主要概况总结iOS多线程中NSThread的解决方式和基本用例

    一.iOS多线程对比

    1. NSThread
      每个NSThread对象对应一个线程,真正最原始的线程
    • 优点:NSThread轻量级最轻,相对简单
    • 缺点:手动管理所有的线程活动,如生命周期、线程同步、睡眠等
    1. NSOperation
      自带线程管理的抽象类
    • 优点:自带线程周期管理,操作上可更注重自己逻辑
    • 缺点:面向对象的抽象类,只能实现它或者使用它定义好的两个子类:NSInvocationOperationNSBlockOperation
    1. GCD
      Grand Central Dispatch 是Apple开发的一个多核编程的解决方法。
    • 优点:最高效,避开并发陷阱
    • 缺点:基于C实现
    1. 选择小结
      • 简单而安全的选择NSOperation实现多线程即可
      • 处理大量并发数据,又追求性能效率的选择GCD
      • NSThread较麻烦,不建议使用

    二. 场景选择

    1. 图片异步加载:这种常见的场景是最常见也是必不可少的,异步加载图片又分成两种
    • 在UI主线程开启新线程按顺序加载图片,加载完成刷新UI
    • 依然是在主线程开启新线程,顺序不定的加载图片,加载完成后刷新UI
    1. 创作工具上的异步:这个跟上边任务调度道理差不多,只是为了丰富描述,有助于“举一反三”效果,如下描述的是APP创作小说
    • app本地创作10个章节内容完成同步服务器,接着同时发表这10个章节将产生的一系列动作,其中上传内容,获取分配章节Id,如果后台没有做处理最好方式做异步按顺序执行。
    • app本地创作列表中有3本小说要发表,如果同时发表创作列表中的3本小说,自然考虑并行队列执行发表。

    三.使用方法

    1. 三种实现开启线程方式:
    • 动态实例化
        NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageSource:) object:imgUrl];
        thread.threadPriority = 1;  //设置线程的优先级(0.0 - 1.0, 1.0最高级)
        [thread start];
    
    • 静态实例化
        
        [NSThread detachNewThreadSelector:@selector(loadImageSource:) toTarget:self withObject:imgUrl];
    
    • 隐式实例化
        [self performSelectorOnMainThread:@selector(loadImageSource:) withObject:self waitUntilDone:imgUrl];
    

    代码示例:

    //
    //  ViewController.m
    //  TestNSThread
    //
    //  Created by taobaichi on 2017/3/21.
    //  Copyright © 2017年 MaChao. All rights reserved.
    //
    
    #import "ViewController.h"
    #define imgUrl @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) UIImageView * imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width /2 - 100, self.view.frame.size.height / 2 - 100, 200, 200)];
        self.imageView.backgroundColor = [UIColor purpleColor];
        [self.view addSubview:self.imageView];
        
        [self dynamicCreateThread];
    }
    
    //动态创建线程
    -(void)dynamicCreateThread{
        NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageSource:) object:imgUrl];
        thread.threadPriority = 1;  //设置线程的优先级(0.0 - 1.0 1.0最高级)
        [thread start];
    }
    
    //静态创建线程
    -(void)staticCreateThread{
        [NSThread detachNewThreadSelector:@selector(loadImageSource:) toTarget:self withObject:imgUrl];
    }
    
    //隐式创建线程
    -(void)implicitCreateThread{
        [self performSelectorInBackground:@selector(loadImageSource:) withObject:imgUrl];
    }
    
    -(void)loadImageSource:(NSString *)url
    {
        NSData * imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        UIImage * image = [UIImage imageWithData:imgData];
        
        if (imgData != nil) {
            //回到主线程刷新UI
            [self performSelectorOnMainThread:@selector(refreshImageView:) withObject:image waitUntilDone:YES];
        }else{
            NSLog(@"there no image data");
        }
    }
    
    -(void)refreshImageView:(UIImage *)image{
        
        [self.imageView setImage:image];
    }
    
    @end
    
    

    2.NSThread的拓展认识

    • 获取当前线程
    NSThread * current = [NSThread currentThread];
    
    • 获取主线程
    NSThread * main = [NSThread mainThread];
    
    • 暂停当前线程
    [NSThread sleepForTimeInterval:2.0];
    
    • 线程之间通信
        //在指定线程上执行
        [self performSelector:@selector(refreshImageView:) onThread:thread withObject:image waitUntilDone:YES];
        
        //在主线程执行
        [self performSelectorOnMainThread:@selector(refreshImageView:) withObject:image waitUntilDone:YES];
    
        //在后台执行
        [self performSelectorInBackground:@selector(refreshImageView:) withObject:image];
        
        //在当前 线程上执行
        [self performSelector:@selector(refreshImageView:) withObject:image];
    

    相关文章

      网友评论

          本文标题:iOS开发多线程篇-NSThread

          本文链接:https://www.haomeiwen.com/subject/uipgnttx.html