一、NSThread优缺点
     优点:NSThread是最轻量级的
     缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销
 
二、NSThread的使用
  • 创建线程:
     + (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullableid)argument; 
     - (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullableid)argument; //argument:传输给target的唯一参数,也可以是nil
     
  1. 直接创建线程,并开始执行线程
     [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
 
  1. 先创建线程对象,然后再运行线程操作,在运行线程操作前可以设置线程的优先级等线程信息
      NSThread* myThread = [[NSThread alloc] initWithTarget:self
                                                  selector:@selector(doSomething:)
                                                    object:nil];
      [myThread start];
     
  1. 不显式创建线程的方法,Swift中去掉了这个方法,苹果认为 performSelector: 不安全,,,,,
      NSObject *obj;
[obj performSelectorInBackground:@selector(doSomething) withObject:nil];
 
例子:
- (void)viewDidLoad {
    [super viewDidLoad];
    //    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
    NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/5366d0160924ab1828b7c95336fae6cd7b890b34.jpg"];
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:url];
    [thread start];
}
 
- (void)downloadImage:(NSString *)url {
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image = [[UIImage alloc]initWithData:data];
    if(image == nil){
       
    }else{
        [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    }
}

- (void)updateUI:(UIImage *)image {
    self.iconView.image = image;

}
 
  1. 更新其他线程
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array 
      - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait 
 
  1. 常用属性
      //取消线程
    - (void)cancel;
    //启动线程
    - (void)start;
    //判断某个线程的状态的属性
    @property (readonly, getter=isExecuting) BOOL executing;
    @property (readonly, getter=isFinished) BOOL finished;
    @property (readonly, getter=isCancelled) BOOL cancelled;
    //设置和获取线程名字
    -(void)setName:(NSString *)n;
    -(NSString *)name;
    //获取当前线程信息
    + (NSThread *)currentThread;
    //获取主线程信息
    + (NSThread *)mainThread;
    //使当前线程暂停一段时间,或者暂停到某个时刻
    + (void)sleepForTimeInterval:(NSTimeInterval)time;
    + (void)sleepUntilDate:(NSDate *)date;
 
 
 

多线程(二)NSThread的更多相关文章

  1. swift开发多线程篇 - NSThread 线程相关简单说明(一些使用和注意点)

    一 说明 本文涉及代码可以从https://github.com/HanGangAndHanMeimei/Code地址获得. 二 NSThread的基本使用和创建 1)基本用法(主线程|当前线程) 1 ...

  2. java 多线程二

    java 多线程一 java 多线程二 java 多线程三 java 多线程四 线程中断: /** * Created by root on 17-9-30. */ public class Test ...

  3. java基础-多线程二

    java基础-多线程二 继承thread和实现Runnable的多线程每次都需要经历创建和销毁的过程,频繁的创建和销毁大大影响效率,线程池的诞生就可以很好的解决这一个问题,线程池可以充分的利用线程进行 ...

  4. iOS开发之多线程(NSThread、NSOperation、GCD)

    整理一些多线程相关的知识. 并行 & 并发 1.并行:并行是相对于多核而言的,几个任务同时执行.2.并发:并发是相对于单核而言的,几个任务之间快速切换运行,看起来像是"同时" ...

  5. ios高级开发之多线程(二)NSThread技术

    多线程技术是有多套解决方案的,那么我们该如何选择呢? 技术方案 简介 语言 线程生命周期 使用频率 pthread 1.一套通用的多线程API 2.适用于UNIX,linux,windows等 3.跨 ...

  6. iOS开发——多线程篇——NSThread

    一.基本使用1.创建和启动线程一个NSThread对象就代表一条线程 创建.启动线程NSThread *thread = [[NSThread alloc] initWithTarget:self s ...

  7. 多线程(NSThread、NSOperation、GCD)编程浅谈

    一.基本概念 进程:一个具有一定独立功能的程序关于某个数据集合的一次运行活动.可以理解成一个运行中的应用程序.线程:程序执行流的最小单元,线程是进程中的一个实体.同步:只能在当前线程按先后顺序依次执行 ...

  8. iOS之多线程开发NSThread、NSOperation、GCD

    原文出处: 容芳志的博客   欢迎分享原创到伯乐头条 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Centr ...

  9. Ios开发之多线程编程——NSThread

    IOS程序在运行的时候是通过主线程来进行UI视图的更新和响应屏幕触摸事件,但是,在视图更新的时候,会有一些非常耗时的工作,这样我们会出现系统出现卡顿的现象,这是因为主线程堵塞造成的,这样会使用户体验非 ...

  10. IOS开发 多线程编程 - NSThread

    每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...

随机推荐

  1. 无法找到类:java.lang.ClassNotFoundException: com.mysql.jdbc.driver

    转载自:http://blog.csdn.net/huangbiao86/article/details/6428608 问题描述:连接数据库,而明明已经将mysql-connector-java-5 ...

  2. 使用mongo-java-driver3.0.2.jar和mongodb3.0在java代码中的用户验证4

    以下是使用mongo-java-driver3.0.2.jar和mongodb3.0.4在java代码中的用户验证: ServerAddress sa = new ServerAddress(host ...

  3. Mysql触发器简明使用

    触发器:trigger创建触发器的语法mysql> delimiter $mysql> create trigger 触发器名称 after/before(触发时间) insert/upd ...

  4. C#学习笔记四: C#3.0自动属性&匿名属性及扩展方法

    前言 这一章算是看这本书最大的收获了, Lambda表达式让人用着屡试不爽, C#3.0可谓颠覆了我们的代码编写风格. 因为Lambda所需篇幅挺大, 所以先总结C#3.0智能编译器给我们带来的诸多好 ...

  5. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  6. PLSQL_闪回操作1_Flashback Query

    2014-07-02 Created By BaoXinjian

  7. pedagogical

    在线考试     // '+this+''; }); //alert(错了); $("#ans").html(html); } function clk(obj){ var inp ...

  8. PDF内容不允许复制的解决方法!

    PDF文档的内容不允许复制解决方法! PDF的加密有两个级别: 一个是Owner级别,就是打开文档时需要输入密码PDF Password Remover可以用来破解Owner级别的口令,但是不能用于破 ...

  9. struts(五) 使用通配符 接收参数

    1.使用通配符简化配置 约定优于配置 <action name="student*" class="com.gc.StudentAction" metho ...

  10. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...