• iOS多线程开发之NSThread


    一、NSThread基本概念

          NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程,需要手动管理线程的生命周期,处理线程同步等问题。

     

    二、创建、启动线程

          1、动态实例化 - 先创建再人工启动

    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadingImage) object:nil];
    
    // 线程启动,在线程thread中执行self的loadingImage方法
    [thread start];   

          2、静态实例化 - 创建后自启动

    // 创建自启动,执行loadingImage方法
    [NSThread detachNewThreadSelector:@selector(loadingImage) toTarget:self withObject:nil];

          3、隐式实例化 - 创建后自启动

    // 创建自启动,执行loadingImage方法
    [self performSelectorInBackground:@selector(loadingImage) withObject:nil];

     

    三、线程控制

         1、暂停 

    + (void)sleepUntilDate:(NSDate *)date;
    + (void)sleepForTimeInterval:(NSTimeInterval)ti;

          NSThread的暂停会阻塞当前线程  

          2、取消

    - (void)cancel

          NSThread的取消线程并不是马上停止并退出线程,只作(线程是否需要退出)状态标记

         3、线程停止

    + (void)exit

         NSThread的停止方法会立即终止除主线程以外所有线程(无论是否在执行任务)并退出,慎用! 否则可能会导致内存问题

     

    四、NSThread的拓展认识

          1、一些常用方法

    // 获得主线程
    + (NSThread *)mainThread;    
    
    // 判断是否为主线程(对象方法)
    - (BOOL)isMainThread;
    
    // 判断是否为主线程(类方法)
    + (BOOL)isMainThread;    
    
    // 获得当前线程
    NSThread *current = [NSThread currentThread];

         2、线程优先级设置

    //iOS8之前
    [NSThread setThreadPriority:1.0];  // (0.0,-1.0,1.0)
     
    -----------------------------分割线------------------------------
    
    //iOS8之后
    [NSThread setQualityOfService:NSQualityOfServiceUserInitiated];
    
    /*
      qualityOfService的枚举值如下:
     NSQualityOfServiceUserInteractive:最高优先级,用于用户交互事件
     NSQualityOfServiceUserInitiated:次高优先级,用于用户需要马上执行的事件
     NSQualityOfServiceDefault:默认优先级,主线程和没有设置优先级的线程都默认为这个优先级
     NSQualityOfServiceUtility:普通优先级,用于普通任务
     NSQualityOfServiceBackground:最低优先级,用于不重要的任务
    */

         3、线程间的通信

    // 1、指定当前线程执行操作
    [self performSelector:@selector(run)];
    [self performSelector:@selector(run) withObject:nil];
    [self performSelector:@selector(run) withObject:nil afterDelay:3.0f];
    
    -----------------------------分割线------------------------------------------
    
    // 2、指定在主线程执行操作(如更新UI)
    [self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
    
    -----------------------------分割线------------------------------------------
    
    // 3、指定在其他线程操作(主线程->新线程)
    
    // 这里指定为某个线程newThread
    [self performSelector:@selector(run) onThread:newThread withObject:nil waitUntilDone:YES]; 
    
    // 这里指定为后台线程
    [self performSelectorInBackground:@selector(run) withObject:nil];

     

    五、线程同步

          多线程不可避免的会带来不同线程并发执行时争夺共享资源的问题(如内存,数据源等),这会造成数据的不一致(脏数据),甚至严重的引起死锁。

          线程同步是指是指在一定的时间内只允许某一个线程访问某个资源,这就像是GCD里的栅栏(dispatch_barrier)或者信号量(dispatch_semphore)一样

          目前iOS实现线程加锁有NSLock和@synchronized两种方式

          关于线程锁的更多相关知识,请参考文章:http://www.jianshu.com/p/35dd92bcfe8c

       

  • 相关阅读:
    vim内外部鼠标复制 粘贴
    nginx 问题解决nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    Installation of the latest version of netease-cloud-music on Fedora 30 linux platform
    Linux就该这么学11学习笔记
    Linux就该这么学10学习笔记
    css day2
    Linux就该这么学09学习笔记
    Linux就该这么学08学习笔记
    css day1
    Linux就该这么学07学习笔记
  • 原文地址:https://www.cnblogs.com/beckwang0912/p/7159329.html
Copyright © 2020-2023  润新知