iOS开发多线程篇-NSThread

2017-03-22  本文已影响35人  码代码的小马

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

一.iOS多线程对比

  1. NSThread
    每个NSThread对象对应一个线程,真正最原始的线程
  1. NSOperation
    自带线程管理的抽象类
  1. GCD
    Grand Central Dispatch 是Apple开发的一个多核编程的解决方法。
  1. 选择小结
    • 简单而安全的选择NSOperation实现多线程即可
    • 处理大量并发数据,又追求性能效率的选择GCD
    • NSThread较麻烦,不建议使用

二. 场景选择

  1. 图片异步加载:这种常见的场景是最常见也是必不可少的,异步加载图片又分成两种
  1. 创作工具上的异步:这个跟上边任务调度道理差不多,只是为了丰富描述,有助于“举一反三”效果,如下描述的是APP创作小说

三.使用方法

  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];
上一篇下一篇

猜你喜欢

热点阅读