iOS开发多线程篇-NSThread

上篇我们学习了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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、优缺点 1.优点:NSThread比其他两种多线程方案较轻量级,更直观地控制线程对象2.缺点:需要自己管理线程...
    lyking阅读 942评论 0 0
  • 原文地址 http://www.cnblogs.com/kenshincui/p/3983982.html 大家都...
    怎样m阅读 5,083评论 0 1
  • 一周六早上,小明处于安全考虑,去银行服务厅申请多一张银行卡作为手机消费指定数额不多的专用卡。到了银行,看到大厅坐满...
    minggo阅读 21,469评论 33 164
  • 小时候,经常在爷爷家住,那里记载了我童年大部分的快乐,记载了我对爷爷的回忆! 小时候听妈妈说,在我爷爷过生日的时候...
    smallSun15阅读 3,085评论 0 1
  • 皮下医学生,能接触的圈子除了饭圈就是医疗圈,所以类比的例子基本就是医疗界。我先不想从哥哥身上入手,先来看看我喜欢的...
    玖蝣阅读 3,012评论 0 0