美文网首页
OC--多线程NSTheard

OC--多线程NSTheard

作者: 啊哈呵 | 来源:发表于2017-09-01 11:11 被阅读21次

    NSThread.h

    @interface NSThread : NSObject
    //当前线程
    @property (class, readonly, strong) NSThread *currentThread;
    //使用类方法创建线程执行任务
    + (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;
    //当前线程优先级
    + (double)threadPriority;
    //设置当前线程优先级
    + (BOOL)setThreadPriority:(double)p;
    //指定线程对象优先级 0.0~1.0,默认值为0.5
    @property double threadPriority NS_AVAILABLE(10_6, 4_0);
    //服务质量
    @property NSQualityOfService qualityOfService NS_AVAILABLE(10_10, 8_0);
    //线程名称
    @property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0);
    //栈区大小
    @property NSUInteger stackSize 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);
    //初始化
    - (instancetype)init NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
    //实例方法初始化,需要再调用start方法
    - (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));
    //线程状态,正在执行
    @property (readonly, getter=isExecuting) BOOL executing NS_AVAILABLE(10_5, 2_0);
    //线程状态,正在完成
    @property (readonly, getter=isFinished) BOOL finished NS_AVAILABLE(10_5, 2_0);
    //线程状态,已经取消
    @property (readonly, getter=isCancelled) BOOL cancelled NS_AVAILABLE(10_5, 2_0);
    //取消,仅仅改变线程状态,并不能像exist一样真正的终止线程
    - (void)cancel NS_AVAILABLE(10_5, 2_0);
    //开始
    - (void)start NS_AVAILABLE(10_5, 2_0);
    //线程需要执行的代码,一般写子类的时候会用到
    - (void)main NS_AVAILABLE(10_5, 2_0);
    @end
    

    NSObject的分类各种performSelector方法

    @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;
    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)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);
    - (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);
    @end
    

    一般用法

        //方法1:使用对象方法
        NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(startNewThread1) object:nil];
        //⚠️注意1:启动一个线程并非就一定立即执行,而是处于就绪状态,当CUP调度时才真正执行
        [thread1 start];
        
        //方法2:使用类方法,自动start
        [NSThread detachNewThreadSelector:@selector(startNewThread1) toTarget:self withObject:nil];
        
        //方法3:隐式创建并启动线程
        [self performSelectorInBackground:@selector(run) withObject:nil];
    

    解析注意

    1.启动一个线程并非就一定立即执行,而是处于就绪状态,当CUP调度时才真正执行,如下图

    相关文章

      网友评论

          本文标题:OC--多线程NSTheard

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