一、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;
 
 
 

最新文章

  1. Greedy:Stripes(POJ 1826)
  2. System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
  3. WinForm 换行问题 textbox (转)
  4. Redis文档
  5. Timus 1446. Sorting Hat 分类问题
  6. 熄灯问题 --POJ 2811-ACM
  7. 简单题思维转化BestCoder
  8. 在ubuntu10.0.4下更新git
  9. BZOJ 1621: [Usaco2008 Open]Roads Around The Farm分岔路口
  10. ubuntu 安装LaTex
  11. nginx https 配置
  12. C#第五天
  13. lua metatable(元表)
  14. 【福大软工】 W班级总成绩排名3
  15. frame buffer简单应用
  16. 用with打开文件
  17. Socket通讯成功案例
  18. linux内核分析ELF文件分析实践报告
  19. JavaScript编码命名规范及格式规范
  20. 检测u盘是否挂载上方法

热门文章

  1. 给windows的VM更换网卡到VMNET3从E1000
  2. 【springBoot】springBoot返回json的一个问题
  3. CGI技术原理
  4. framework 安装出错 1603
  5. C#使用ConditionalAttribute特性来实现代码调试
  6. 黄聪:HtmlAgilityPack,C#实用的HTML解析类简介
  7. capture同focus
  8. C和C++混合编程
  9. linux命令(13) 删除指定文件夹下后缀名相同的文件
  10. 转 -android:程序无响应,你该如何定位问题?