多线程(NSThread+NSOperation)

多线程(NSThread+NSOperation)

多线程概述

  • 程序:由源代码生成的可执行的应用
  • 进程:一个正在运行的程序可一件做一个进程.进程拥有独立运行所需的全部资源
  • 线程:程序中独立运行的代码块
  • 一个进程由至少一个线程组成,进程负责资源的调配,线程才是程序真正执行的单元,负责代码的执行
  • 只有一个主线程的程序叫做单线程程序
  • 主线程负责UI展现及刷新,本地存储(一般情况下图片缓存,数据缓存在客户端上运行的数据很少,所以放到主线程)
  • 子线程和主线程都是独立的运行单元,各自的执行互补影响,所以能够并发执行
  • 开启多个线程并不能提高程序的运行性能,所以要适当开启线程

NSThread

方法 功能
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument 初始化一个子线程,但是需要手动开启
+ (void)detachNewThreadSelector:(SEL)aselector toTarget:(id)aTargetWithObject:(id)anArgument 初始化一个子线程,不需要手动开启
start 开启子线程
cancel 取消当前子线程
[NSThread currentThread] 获取当前线程
[NSThread mainThread] 获取主线程
[NSThread sleepForTimeInterval:] 线程休眠(秒)
  • 第一种创建方式
    //创建一个线程
    NSThread  *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread:) object:nil];
    thread.name = @"第一个子线程";
    //设置线程的优先级
    thread.threadPriority  = 0.9;
    //开启
    [thread start];
  • 第二种创建方式
    //再添加一个线程(自动开启,不许要手动开启)
    [NSThread detachNewThreadSelector:@selector(thread2:) toTarget:self withObject:nil];
    //是否是主线程
    BOOL ismain = [NSThread isMainThread];
    NSLog(@"%d",ismain);
    //睡眠
    [NSThread sleepForTimeInterval:3];
  • 线程的取消和发送的通知(三种通知,只要线程在执行相应的操作,就会发送通知)
    //变成多线程通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isMultipelThread) name:NSWillBecomeMultiThreadedNotification object:nil];
    //变成单线程通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isSingleThread) name:NSDidBecomeSingleThreadedNotification object:nil];
    //推出线程通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isExitThread) name:NSThreadWillExitNotification object:nil];
    //创建一个线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread) object:nil];
    [thread start];//开启
    [thread cancel];//取消
  • 线程的隐式创建和通信(一般将耗时操作挪到子线程中,但是刷新UI要返回主线程操作)
#pragma mark----------NSThread的隐式创建
    //开启子线程
    [self performSelectorInBackground:@selector(background1) withObject:nil];
    //子线程执行的方法
#pragma mark----------NSThread的隐式创建
//子线程执行的方法
- (void)background1
{
    NSLog(@"当前线程 %@",[NSThread currentThread]);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic.nipic.com/2007-11-09/200711912453162_2.jpg"]];
    UIImage*image = [UIImage imageWithData:data];
    //回主线程(第一种方式)
    [self performSelectorOnMainThread:@selector(mainThread:) withObject:image waitUntilDone:YES];
    //回到指定线程(第二种方式)
    //先拿到主线程    yes是先执行selector中的方法,在执行下面的东西 no就是不管selector方法中的有没有执行完,都要接着执行
NSThread *thread =    [NSThread mainThread];
    [self performSelector:@selector(mainThread:) onThread:thread withObject:image waitUntilDone:YES];
}
- (void)mainThread:(UIImage *)image
{
    UIImageView *view = [[UIImageView alloc] initWithImage:image];
    view.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:view];
    NSLog(@"mainThred = %@",[NSThread currentThread]);
}

通知机制

  • 通知中心(NSNotificationCenter)用来操作通知(NSNotification)的
  • 通知有三个属性(name,object,userInfo)(后两个都可以用来传值)
  • 通知中心最主要的用法是可以多对多的进行通信,发送通知的一方,发送了通知,只要注册了相对应的通知的一方都会收到通知,同理,注册通知的一方也可以收到多个来自于不同name发送的通知
  • 通知中心一定要有注册通知和发送通知,
  • 通知中心是单例,可以通过[NSNotificationCenter defaultCenter]创建
  • 通知中心注册的时候就会持有当前的控制器(self),所以要记得手动移除(在注册的一方,移除一次就好)
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject
  //注册通知 1. 监听者(消息的接收者) 2.接收到消息执行的方法 3.通知的名字 4.配置信息(附加信息)(注册的时候一定要想着移除)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aaa:) name:@"name" object:nil];
#pragma mark----------通知机制
    - (void)aaa:(NSNotification*)object
{
    NSLog(@"接收到通知");
    NSLog(@"%@",object.object);
}
- (void)dealloc
{
    //一定要移除
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
---------------这里已经是第二个控制器了
//通过按钮跳转回前一个页面时,传值
- (void)bbb
{
    //发送通知(发送可以一对多,只要发送,注册过的界面都会接收到通知)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:@"789456"];
    //跳回上一个界面
    [self dismissViewControllerAnimated:YES completion:nil];
}

NSOperation

  • NSOperation是一个抽象的类,不能够直接使用,而是使用子类(NSInvocationOperation或NSBlockOperation),这两个子类前者是通过添加target触发事件去执行方法,一个是通过block方法
  • NSOperationQueue是一个操作队列,用它来管理一组任务的执行(也就是多个Operation),他会根据需要自动为任务开辟适合数量的线程,并执行
  • 队列中的任务遵循先进先出的原理(先进不一定先执行完,所以'先出'指的是先执行)
  • 可以通过调节任务在队列中的优先级来控制任务的执行顺序
  • 只要是自己创建的队列(NSOperationQueue),就会在子线程中执行,并且默认是并发
  • NSOperationQueue的主要方法和属性
方法 功能
- (void)addOperation:(NSOperation *)op 添加任务
- (void)addOperationWithBlock:(void (^)(void))block 添加任务(用block)添加
maxConcurrentOperationCount 最大并发数
suspended 是否暂停队列
operationCount 队列中的任务数量
operations 队列中的所有任务(数组管理)
cancelAllOperations 取消所有任务

NSBlockOperation

    //创建任务(第一种)
    NSBlockOperation *blockOperation  =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"当前线程  %@",[NSThread currentThread]);
    }];
    //加入队列里面(自动开启任务)
    NSOperationQueue *queue  =[[NSOperationQueue alloc] init];
    [queue addOperation:blockOperation];
    //直接创建队列并添加block(第二种)
        [queue addOperationWithBlock:^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        //回主线程
        [self performSelectorOnMainThread:@selector(aaa) withObject:nil waitUntilDone:YES];
            //添加依赖(第二个依赖于第一个完成之后才能执行)
    [queue.operations[1] addDependency:queue.operations[0]];
    }

NSInvocationOperation

    NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invo) object:nil];
    [queue addOperation:invo];


    
    - (void)invo
{

    NSLog(@"%@",[NSThread currentThread]);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容