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<NSNumber *> *callStackReturnAddresses;//线程函数地址
@property (class, readonly, copy) NSArray<NSString *> *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 = -
} 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<NSString *> *)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<NSString *> *)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

iOS开发线程之NSThread的更多相关文章

  1. iOS多线程之NSThread使用

    iOS中的多线程技术 我们在iOS开发项目过程中,为了解决UI界面操作不被耗时操作阻塞,我们会使用到多线程技术.在iOS开发中,我们主要会用到三种多线程操作技术:NSThread,NSOperatio ...

  2. iOS多线程之NSThread详解

    在iOS中每个进程启动后都会建立一个主线程(UI线程),这个线程是其他线程的父线程.由于iOS中除了主线程,其他子线程是独立于Cocoa Touch的,所以只有主线程可以更新UI界面.iOS多线程的使 ...

  3. 【原】iOS多线程之NSThread、NSOperationQueue、NSObject和GCD的区别

    区别: Thread: 是这几种方式里面相对轻量级的,但也是使用起来最负责的,你需要自己管理thread的生命周期,线程之间的同步.线程共享同一应用程序的部分内存空间, 它们拥有对数据相同的访问权限. ...

  4. ios 多线程之NSThread篇举例详解

    这篇博客是接着总篇iOS GCD NSOperation NSThread等多线程各种举例详解写的一个支篇.总篇也包含了此文的链接.本文讲解的知识点有NSThread的开始.取消.在当前线程执行任务. ...

  5. IOS多线程之NSThread

    参考:http://blog.csdn.net/totogo2010/article/details/8010231 1 简介 NSThread: 优点:NSThread 比其他两个轻量级 缺点:需要 ...

  6. iOS多线程之GCD小记

    iOS多线程之GCD小记 iOS多线程方案简介 从各种资料中了解到,iOS中目前有4套多线程的方案,分别是下列4中: 1.Pthreads 这是一套可以在很多操作系统上通用的多线程API,是基于C语言 ...

  7. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  8. 多线程之NSThread

    关于多线程会有一系列如下:多线程之概念解析 多线程之pthread, NSThread, NSOperation, GCD 多线程之NSThread 多线程之NSOperation 多线程之GCD一, ...

  9. iOS多线程之Thread

    多线程 • Thread 是苹果官方提供的,简单已用,可以直接操作线程对象.不过需要程序员自己管理线程的生命周期,主要是创建那部分 优缺点 面向对象,简单易用 直接操作线程对象 需要自己管理线程生命周 ...

随机推荐

  1. fedora 28 winscp链接不上

    systemctl restart sshd.service //启动sshd服务 systemctl stop firewalld //关闭防火墙 /etc/selinux/config //关闭s ...

  2. margin与padding

    1.不加内边距的div: <div style="width:150px; height:150px; ">    <div style="width: ...

  3. BCZM : 1.16

    24点游戏 解法一:穷举法 解法二:分治法

  4. 覆盖element ui 的样式

    我们可以使用 !important  来覆盖element ui 的样式 首先先在浏览器中找到 我们所要修改的样式 ,然后找到她的 class  重新写他的样式 ,例如 . app  { width ...

  5. vue 学习 cli3常用配置

    ---恢复内容开始--- cli3以后,构建的项目更加的简洁,配置文件也没有向cli2那样暴漏出来,但这并不代表cli3是不可配置的,我们只需要在根目录下添加一个vue.config.js作为项目的配 ...

  6. IconFont 图标的3种引用方式

    第一步:进入阿里巴巴矢量图网站:http://www.iconfont.cn/   阿里巴巴矢量图 第二步:搜索你分类的关键字---然后加入购物车,下载到本地,然后解压,会将合并后的字体文件及自动生成 ...

  7. C#/.NET 实现的多屏联动,多屏共享,显示到指定屏幕上

    假设我现在有4个屏幕,希望实现主屏幕在操作的时候,其他3块屏幕可以实时联动,并且延迟在1s以内. 正常情况下,我们可以借助于各个远程软件实现效果,但是有时候会显得笨重麻烦,假如只有一台服务器呢?? S ...

  8. 脚本启动SpringBoot(jar)

    #!/bin/sh RESOURCE_NAME=springbsit-api.jar tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kil ...

  9. 8-26接口压力测试-3Jmeter-Java请求

    1.新建maven工程 2.导入依赖,并使用shade将所需的依赖打入jar包 <?xml version="1.0" encoding="UTF-8"? ...

  10. Zookeeper_Paxos算法

    Paxos算法 算法简介 Paxos算法是莱斯利-兰伯特在1990年提出的一种基于消息传递的.具有容错性的一致性算法,Google Chubb(分布式锁服务)的作者Mike Burrwos说过,世上只 ...