博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发线程之NSThread
阅读量:6943 次
发布时间:2019-06-27

本文共 4463 字,大约阅读时间需要 14 分钟。

1、初始化

- (instancetype)init API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) NS_DESIGNATED_INITIALIZER;- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.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;- (void)start API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));- (void)main API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));    // thread body method

  特点:main

//执行start方法后会自动调用main方法。//main是默认的初始化和调用selector的方法。如果要继承NSThread,可以重写main方法来执行新线程的主要部分。重写的mian方法不需要调用super。不要直接调用mian方法,而是通过start方法来调用。

3、停止线程

+ (void)sleepUntilDate:(NSDate *)date;//休眠到指定时间+ (void)sleepForTimeInterval:(NSTimeInterval)ti;//休眠多久+ (void)exit;//退出所有线程- (void)cancel API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));//取消线程

  特点:exit

//exit方法先执行currentThread类方法来获取当前线程。//由于退出线程前给默认通知中心发送NSThreadWillExitNotification通知,并把当前线程作为参数,因为post通知是同时发送的,所以所有NSThreadWillExitNotification 通知的观察者都会收到通知,导致所有线程都退出。

4、描述线程状态

@property (readonly, getter=isExecuting) BOOL executing;//是否在执行@property (readonly, getter=isFinished) BOOL finished ;//是否执行完毕@property (readonly, getter=isCancelled) BOOL cancelled;//是否已经取消,此时finished可能为NO

5、主线程和多线程

@property (readonly) BOOL isMainThread ;//是否是主线程@property (class, readonly, strong) NSThread *mainThread ;//获取当前主线程+ (BOOL)isMultiThreaded;//是否是多线程@property (class, readonly, strong) NSThread *currentThread;//当前线程 1为主线程@property (class, readonly, copy) NSArray
*callStackReturnAddresses;//线程函数地址@property (class, readonly, copy) NSArray
*callStackSymbols;//当前线程的调用栈

6、属性设置

//可以使用返回的字典来保存线程的特定数据。这只是一个普通的字典,用来保存所有开发者感兴趣的数据。@property (readonly, retain) NSMutableDictionary *threadDictionary;//线程的堆内存大小字节数。必须是4KB的倍数。要使设置有用,必须在start方法调用前设置。@property NSUInteger stackSize;@property (nullable, copy) NSString *name;//线程名字

7、优先级

+ (double)threadPriority;//当前线程优先级【0.0--1.0】默认0.5+ (BOOL)setThreadPriority:(double)p;//设置优先级成功返回YES@property double threadPriority API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); // To be deprecated; use qualityOfService below@property NSQualityOfService qualityOfService API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)); // read-only after the thread is started
typedef NS_ENUM(NSInteger, NSQualityOfService) {    /* 最高优先级,主要用于提供交互UI的操作,比如处理点击事件,绘制图像到屏幕上 */    NSQualityOfServiceUserInteractive = 0x21,    /* 次高优先级,主要用于执行需要立即返回的任务 */    NSQualityOfServiceUserInitiated = 0x19,    /* 普通优先级,主要用于不需要立即返回的任务*/    NSQualityOfServiceUtility = 0x11,    /* 后台优先级,用于完全不紧急的任务*/    NSQualityOfServiceBackground = 0x09,    /* 默认优先级,当没有设置优先级的时候,线程默认优先级*/    NSQualityOfServiceDefault = -1} API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

8、通知

FOUNDATION_EXPORT NSNotificationName const NSWillBecomeMultiThreadedNotification;//由当前线程派生出第一个其他线程时发送,一般一个线程只发送一次FOUNDATION_EXPORT NSNotificationName const NSDidBecomeSingleThreadedNotification;//未知。FOUNDATION_EXPORT NSNotificationName const NSThreadWillExitNotification;//当线程对象获取到exit消息时,广播这个通知。

9、NSThreadPerformAdditions类别

@interface NSObject (NSThreadPerformAdditions)- (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;// 用于线程通信,子线程传到主线程,默认的运行时模式: kCFRunLoopCommonModes- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray
*)array API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));//用于线程之间的通信- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));//用于线程之间的通信- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));// 隐式创建线程,默认的运行时模式 kCFRunLoopCommonModes@end

 

转载于:https://www.cnblogs.com/xianfeng-zhang/p/9360288.html

你可能感兴趣的文章
关于江苏水文分析评价项目阶段总结会议
查看>>
VBS基础篇 - 运算符(4) - 比较运算符
查看>>
evernote 2.2 搜索的问题
查看>>
初学正则表达式之不可忽视的空白符
查看>>
农二代蚁族寄居大城市的代价 是不是有点太惨重了
查看>>
获取MSSQL / MYSQL的已用容量
查看>>
使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)
查看>>
C#窗体读取EXCEL存入SQL数据库
查看>>
HDU 3436 Queue-jumpers
查看>>
禁止apache显示目录索引的常见方法(apache禁止列目录)
查看>>
php之道
查看>>
Jquery实现Bootstrap树形列表
查看>>
linux下安装nginx
查看>>
钉钉js依赖库学习
查看>>
转载mysql数据库配置优化
查看>>
Perl图书的一些体会
查看>>
阿里Java开发规范&谷歌Java开发规范&华为Java开发规范&Tab键和空格比较&Eclipse的Tab键设置 总结...
查看>>
android电话状态的监听
查看>>
Linq中string转int的方法
查看>>
循环-12. 打印九九口诀表(15)
查看>>