一、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. Ubuntu Server上的LVM配置

    在安装Linux的时候,通常遇到的一个比较头痛的问题就是分区,到底每个区该分多少,用了一段时间之后,某个分区又不够用了,该怎么办?如果是普通的服务器,那一切都好说,大不了就关机重新划分分区嘛,但是对于 ...

  2. 黄聪:解决丢失api-ms-win-crt-runtime-|1-1-0.dll的问题:vc_redist.x64

    解决无法启动程序,因计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll的问题 安装:Microsoft Visual C++ 2015 RC Redistributable ...

  3. PHP 打印调用函数入口地址(堆栈)

    今天网站出现一个BUG,然后直接在数据库类里面写日志,看是哪条SQL出了问题,SQL语句到是找到了,但是不知道这条SQL语句来自何处,于是就想啊,如果能有一个办法,查看当前正在运行的这个方法是被哪个方 ...

  4. difference between instance variable and property

    @interface MyClass : NSObject { NSString *name; NSArray *items; Something *something; IBOutlet NSTex ...

  5. application in 2014

    OA WEBSITE ERP ISO ANDROID EF+MVC4+CATCHE+JQuery+js+div+css+web性能优化+webservice+sql2008+设计模式+wcf+多线程 ...

  6. Web上传文件

      客户端      相对于FTP文件上传,Web文件上传速度慢一些,但使用方便,不需要客户端,而且权限比FTP容易控制. Web文件上传采用POST方式,上传文件需要设置FORM的entype属性为 ...

  7. IOS Bugs5 linker command failed with exit code 1 (use -v to see invocation)

    Ld /Users/Rubert/Library/Developer/Xcode/DerivedData/OC_Language-emftyzftyvhdpuaxipddjmpnpvox/Build/ ...

  8. set_union的几个例子

    获得两个集合的并集.两个输入序列须保证已排好序. 数组用的时候 // set_union example #include <iostream> #include <algorith ...

  9. Redefining already defined constructor

    报错:Strict standards: Redefining already defined constructor for class SplitWord in D:\wamp\www\wsc\i ...

  10. 【Java】图片高质量缩放类

    package com.test; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg. ...