iOS开发多线NSThread(三)

NSThread是GCD,NSThread,NSOperationQueue三种方法里面相对轻量级的,但需要管理线程的生命周期、同步、加锁问题,这会导致一定的性能开销

一、NSThread简单介绍苹果开发文档链接

1、对象方法初始化线程

- (instancetype)init NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument NS_AVAILABLE(10_5, 2_0);
- (instancetype)initWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
2、类方法初始化线程

+ (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;

3、常用参数

// 设置线程的优先级(0.0 - 1.0,1.0最高级)
@property double threadPriority NS_AVAILABLE(10_6, 4_0);
//给线程起名字
@property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0);
//判断是否为主线程(类方法)
@property (class, readonly) BOOL isMainThread NS_AVAILABLE(10_5, 2_0); 
//获取主线程(类方法)
@property (class, readonly, strong) NSThread *mainThread NS_AVAILABLE(10_5, 2_0);
4、线程通信

//在主线程上执行操作
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
//在指定线程上执行操作
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)array NS_AVAILABLE(10_5, 2_0);
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);
//立即创建一个线程并执行selector方法多用于在服务器端获取完数据通过后台使用多线程方式自动更新UI
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);

二、NSThread简单使用

1、下载图片测试
- (IBAction)downloadImageClick:(id)sender {
    BOOL result = [NSThread isMainThread];
    NSLog(@"downloadImageClick%d",result);
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(download:) object:nil];
    thread.name = @"downloadthread";
    [thread start];
    [NSThread detachNewThreadWithBlock:^{
        NSThread *th = [NSThread currentThread];
        NSLog(@"threadName - %@",th);
    }];
}

- (void)download:(NSThread *)thread{
    BOOL result = [NSThread isMainThread];
    NSLog(@"download%d",result);
    NSLog(@"threadName - %@",[NSThread currentThread].name);
#define IMAG_URL @"http://avatar.csdn.net/C/2/6/1_u010067452.jpg"
    NSString *url = [IMAG_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *image = [UIImage imageWithData:data];
    //线程通信
    NSLog(@"before");
    [self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    //waitUntilDone 是否不等待主线程指向完该方法再往下执行
    //no 子线程不等待主线程 直接向下继续运行
    //yes 子线程需要等待 主线程执行完setImage方法再执行
    NSLog(@"after");
}

- (void)setImage:(UIImage *)image{
    //主线程执行 image子线传递 完成线程之间的通信
    NSLog(@"setImage");
    self.imageView.image = image;
    
}

2、exit使用
- (void)test{
    //(1)通过类方法开辟子线程
    NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(printA) object:nil];
    [thread1 start];//开始执行printA 执行过程中 线程间是相互独立 不会主线程的正常执行
//    [thread1 cancel];//取消
    //【注意】cancel取消线程 并不是将线程销毁 而是给线程打上一个标记 根据线程的取消标记 判断是否需要对线程做一些处理 打上标记后 线程依然正常执行 指导执行结束退出
    [NSThread exit];//子线程退出 结束 不是对象方法 而是类方法 主动退出
    [NSThread detachNewThreadSelector:@selector(printB) toTarget:self withObject:nil];
    //一旦创建 自动启动 不需要发送任何启动子线程的指令
}
- (void)printB{
  
    for (int i = 0; i < 5; i++) {
        NSLog(@"printB");
    }
}

- (void)printA{
    for(int i = 0; i < 5; i ++){
        NSLog(@"printA");
    }
}


打印结果为

2017-03-20 16:42:24.934 NSThreadProject[6911:303193] printA
2017-03-20 16:42:24.935 NSThreadProject[6911:303193] printA
2017-03-20 16:42:24.935 NSThreadProject[6911:303193] printA
2017-03-20 16:42:24.935 NSThreadProject[6911:303193] printA
2017-03-20 16:42:24.936 NSThreadProject[6911:303193] printA





你可能感兴趣的:(iOS程序开发)