一、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. Saltstack系列5:Saltstack之pillar组件

    pillar组件 pillar也是Saltstack最重要的组件之一,其作用是定义与被控主机相关的任何数据,定义好的数据可以被其他组件使用,如模板.state.API等. 在pillar中定义的数据与 ...

  2. 装过photoshop后出现configuration error

    1.你用的应该是精简版的PS,找到ps启动图标,点击右键,以管理员身份运行试试. 2.可以右键你的快捷方式,选择兼容性,后面有个选框“以管理员身份运行”,应用,下次就不报错了.

  3. RDP setting group policy

    RDP setting group policy 1.Login to domain controller and go to Group Policy Management tool2.Click ...

  4. 再看erlang的socket部分基础

    socket的选项里面的{packet,0}和{packet,raw}的区别 {packet,} erlang处理2字节大端包头 {packet,} erlang处理4字节大端包头 {packet,} ...

  5. Apple dev travel

    Objective-C最基础语法之Class定义: http://mobile.51cto.com/iphone-281925.htm  Table View: http://www.appcoda. ...

  6. Maven使用-- 编写POM

        就像Make的Makefile.Ant的build.xml一样,Maven项目的核心是pom.xml. POM(Project Object Model,项目对象模型)定义了项目的基本信息,用 ...

  7. [运维-服务器 – 2A] – nginx下绑定域名

    这个篇文章今天(2016-01-21)才有幸写了,因为自己对nginx部署以前没玩过,还得感谢下我们数字化的总经理.在这里记录下自己成长的经验,与遇到的问题. 因为自己的域名是在万网上买的,解析无法直 ...

  8. cf380D Sereja and Cinema 组合数学

              time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  9. suibi11172

    http://blog.sina.com.cn/s/blog_12f37f0f00102wi8q.html 七.Python列表操作的函数和方法 列表操作包含以下函数: 1.cmp(list1, li ...

  10. php 消息队列

    本消息队列用于linux下,进程通信 #根据路径和后缀创建一个id $key = ftok(__DIR__, 'R'); #获取队列中的消息 $q = msg_get_queue($key); #删除 ...