美文网首页
多线程之NSThread

多线程之NSThread

作者: 云天涯丶 | 来源:发表于2018-04-12 13:46 被阅读10次

    一、介绍
    NSThread用来创建多线程、监视线程的运行时情况等,线程管理需要程序员自己手动操作。

    二、API

    @interface NSThread : NSObject  {
    @private
        id _private;
        uint8_t _bytes[44];
    }
    
    // 当前线程
    @property (class, readonly, strong) NSThread *currentThread;
    
    // 类方法 不用调用start即可执行
    + (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;
    
    // 应用是否是多线程
    + (BOOL)isMultiThreaded;
    // 线程存储的信息 可以自己写入一些数据
    @property (readonly, retain) NSMutableDictionary *threadDictionary;
    
    // 睡眠(阻塞线程)
    + (void)sleepUntilDate:(NSDate *)date;
    + (void)sleepForTimeInterval:(NSTimeInterval)ti;
    
    // 退出当前线程
    + (void)exit;
    
    //  线程优先级:决定线程被CPU调用的频率,不保证调用的顺序
    + (double)threadPriority;
    + (BOOL)setThreadPriority:(double)p;
    @property NSQualityOfService qualityOfService; // read-only after the thread is started
    
    // 堆区的一些信息
    @property (class, readonly, copy) NSArray<NSNumber *> *callStackReturnAddresses;
    @property (class, readonly, copy) NSArray<NSString *> *callStackSymbols;
    
    @property (nullable, copy) NSString *name;
    
    @property NSUInteger stackSize;
    
    @property (readonly) BOOL isMainThread;
    @property (class, readonly) BOOL isMainThread; // reports whether current thread is main
    @property (class, readonly, strong) NSThread *mainThread;
    
    // 实例方法创建对象
    - (instancetype)init;
    - (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument;
    - (instancetype)initWithBlock:(void (^)(void))block;
    
    // 执行
    @property (readonly, getter=isExecuting) BOOL executing;
    // 完成
    @property (readonly, getter=isFinished) BOOL finished;
    // 取消
    @property (readonly, getter=isCancelled) BOOL cancelled ;
    
    - (void)cancel;
    
    - (void)start;
    
    - (void)main;   // thread body method
    
    @end
    
    // 通知相关
    FOUNDATION_EXPORT NSNotificationName const NSWillBecomeMultiThreadedNotification;
    FOUNDATION_EXPORT NSNotificationName const NSDidBecomeSingleThreadedNotification;
    FOUNDATION_EXPORT NSNotificationName const NSThreadWillExitNotification;
    
    @interface NSObject (NSThreadPerformAdditions)
    
    // 在主线程搞事情
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
        // equivalent to the first method with kCFRunLoopCommonModes
    
    // 在某个线程搞事情
    - (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;
        // equivalent to the first method with kCFRunLoopCommonModes
    
    // NSObject创建子线程
    - (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
    
    @end
    

    注意点:
    1、exit:谨慎使用:
    ①、不要再主线程使用
    ②、调用该方法时先清理线程中的资源,防止内存泄漏

    2、- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
    参数理解:
    aSelector:SEL,一个方法名
    arg:参数
    wait:yes会阻塞当前线程,no当前线程继续执行
    array:runloop的mode

    例子:

    - (void)threadDemonstration{
        [NSThread detachNewThreadWithBlock:^{
            for (int i = 0; i < 10; i++) {
                if (i == 6) {
                    // yes会阻塞当前线程,no当前线程继续执行
                    [self performSelectorOnMainThread:@selector(check) withObject:nil waitUntilDone:YES];
                }
                NSLog(@"%@在执行%d",[NSThread currentThread],i);
            }
        }];
    
    }
    
    - (void)check{
        
        NSLog(@"haha");
    }
    

    wait:YES时:check会先于NSLog(@"%@在执行%d",[NSThread currentThread],i);执行


    屏幕快照 2018-04-12 下午1.26.22.png

    wait:NO时:check最后执行


    屏幕快照 2018-04-12 下午1.28.11.png

    3、main
    线程的主要入口。
    如果继承NSThread,则可以重写此方法,并使用它来实现线程的主体。 如果这样做,不需要调用super。

    注:这篇文章写的比较详细,可作参考

    相关文章

      网友评论

          本文标题:多线程之NSThread

          本文链接:https://www.haomeiwen.com/subject/ccdxkftx.html