Objc多线程-NSThread

NSThread

查看NSThread.h中的接口:

类方法不返回NSThread实例,直接在其他线程里面执行任务。

+ (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:(nullableid)argument;


实例方法返回NSThread对象,需要调用start,或者main才能执行,执行完任务,ARC自动回收NSThread对象。

- (instancetype)init NS_AVAILABLE(10_5,2_0) NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullableid)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));


线程优先级,类方法,实例化NSThread后调用,优先级高的任务先被调度。这个属性即将废弃,改为qualityOfService。

+ (double)threadPriority;

+ (BOOL)setThreadPriority:(double)p;

@property double threadPriority NS_AVAILABLE(10_6,4_0);// To be deprecated; use qualityOfService below

@property NSQualityOfService qualityOfService NS_AVAILABLE(10_10,8_0);// read-only after the thread is started

线程Sleep,类方法,将当前线程sleep。

+ (void)sleepUntilDate:(NSDate *)date;

+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

NSObject (NSThreadPerformAdditions):以下category方法,在delay后,把事件放到runloop里面,runloop通知相应的线程去执行。这套机制生效的前提是onThread的thread的runloop是开启的。如果子线程runloop未开启,就不会执行selector,如果waitUntilDone为YES,此时就会阻塞当前线程。

@interface NSObject (NSThreadPerformAdditions)

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait;

// equivalent to the first method with kCFRunLoopCommonModes

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)array NS_AVAILABLE(10_5,2_0);

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullableid)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5,2_0);

// equivalent to the first method with kCFRunLoopCommonModes

- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullableid)arg NS_AVAILABLE(10_5,2_0);

start方法和main方法

You should never invoke this method directly. You should always start your thread by invoking the start method.

开启两个线程NSThread,调用main方法,两个NSThread中的任务顺序由主线程执行。没有起到在子线程中并发。所有不要显式的调用main方法,如[thread main];

一旦NSThread的子类重写了main()方法,[thread start]就只会调用main方法里面的任务,block和selector都不会执行。

- (void)cancel NS_AVAILABLE(10_5,2_0);

- (void)start NS_AVAILABLE(10_5,2_0);

- (void)main NS_AVAILABLE(10_5,2_0);// thread body method

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 开启线程 分离主线程创建:创建线程后会自动执行,但是线程外部不可获取到该线程对象detachNewThreadWi...
    Mr_Pt阅读 1,078评论 0 1
  • 1.术语线程:用于指代独立执行的代码段进程:用于指代一个正在运行的可执行程序,它可以包含多个线程。任务:用于指代抽...
    Jason_KB阅读 204评论 0 0
  • 一、多线程简介: 所谓多线程是指一个 进程 -- process(可以理解为系统中正在运行的一个应用程序)中可以开...
    寻形觅影阅读 1,068评论 0 6
  • 因为什么,我最新重新学习了多线程的知识,顺便总结一下。 首先,为什么要使用多线程,在iOS中我们希望主线程可以快速...
    yezi1989阅读 237评论 0 1
  • 木木: 想到给你写信如何称呼你的时候,脑海中不自觉的就浮现出这个名字。这是你曾经玩网游时的网名,我这样的称呼了你很...
    梅子Mey阅读 287评论 4 4