You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//类属性
//获取当前线程信息(通过用于调试)
class var current: Thread { get }
//是否是主线程(通过用于调试,判断当前线程是否是主线程)
class var isMainThread: Bool { get }
//获取主线程,一般用于在子线程执行完任务后,回到主线程中执行任务
class var main: Thread { get }
//获取当前线程的调用栈
class var callStackSymbols: [String] { get }
//该线程调用函数的名字数字
class var callStackReturnAddresses: [NSNumber] { get }
//属性
var name: String? 线程名称
var stackSize: Int 线程使用栈区大小(默认是512K)
var isMainThread: Bool { get } 是否是主线程
var isExecuting: Bool { get } 是否正在被执行
var isFinished: Bool { get } 是否已经完成
var isCancelled: Bool { get } 是否取消
var threadPriority: Double 设置线程优先级(0-1.0)
var qualityOfService: QualityOfService 线程服务优先级
//每个线程都有个字典,在线程中任何地方被访问
var threadDictionary: NSMutableDictionary { get }
//类方法
//线程终止(在执行任务过程中如果调用该方法,会使线程进入死亡状态)
class func exit()
class func isMultiThreaded() -> Bool 当前代码运行所在线程是否是子线程
class func threadPriority() -> Double 线程优先级
class func sleep(until date: Date) 让线程睡眠多长时间(单位是Date)
class func setThreadPriority(_ p: Double) -> Bool 设置优先级
//创建线程(iOS 10.0
class func detachNewThread(_ block: @escaping () -> Void)
//让线程睡眠多长时间(单位是TimeInterval)
class func sleep(forTimeInterval ti: TimeInterval)
class func detachNewThreadSelector(_ selector: Selector, toTarget
target: Any, with argument: Any?)
//方法
func cancel() 取消执行
func start() 开始执行
func main() 获取主线程
init() 初始化方法
convenience init(block: @escaping () -> Void) (iOS 10.0)
convenience init(target: Any, selector: Selector, object argument: Any?)
//convenience 便利构造函数特点
1便利构造函数通常都是写在extension里面
2便利函数init前面需要加载convenience
3在便利构造函数中需要明确的调用self.init()
NSLog("开始了")
let thread1 = Thread(target: self, selector: #selector(method1), object: nil)
let thread2 = Thread(target: self, selector: #selector(method2), object: nil)
thread1.threadPriority = 0.3
thread2.threadPriority = 0.8
thread1.start()
thread2.start()
NSLog("完成了")
@objc func method1() {
for _ in 0...2 {
NSLog("11111--\(Thread.current)--\(Thread.threadPriority())")
}
}
@objc func method2() {
for _ in 0...2 {
NSLog("22222--\(Thread.current)--\(Thread.threadPriority())")
}
}
打印结果: 开始了
完成了
22222--<NSThread: 0x60000064c540>{number = 7, name = (null)}--0.8064516129032258
11111--<NSThread: 0x60000064c800>{number = 6, name = (null)}--0.3064516129032258
22222--<NSThread: 0x60000064c540>{number = 7, name = (null)}--0.8064516129032258
11111--<NSThread: 0x60000064c800>{number = 6, name = (null)}--0.3064516129032258
22222--<NSThread: 0x60000064c540>{number = 7, name = (null)}--0.8064516129032258
11111--<NSThread: 0x60000064c800>{number = 6, name = (null)}--0.3064516129032258
threadPriority(Double类型)默认的优先级是0.5,优先级取值范围是0-1.0,设置优先级只能去控制多个线程之间哪个线程先开始执行任务,而不能控制任务的真实顺序;优先级高的线程里面的任务最先执行.The priorities in this range are mapped to the operating system's priority values. A “typical” thread priority might be 0.5, but because the priority is determined by the kernel, there is no guarantee what this value actually will be.这是苹果给的说明,意思就是优先级的值是由内核决定的,它确切的值不能保证是多少
6. 服务优先级 qualityOfService
苹果文档是这么解释的:Used to indicate the nature and importance of work to the system. Work with higher quality of service classes receive more resources than work with lower quality of service classes whenever there is resource contention.意思就是标识这个任务的重要性,每当存在资源竞争时,服务质量高的任务将获得更多的资源
public enum QualityOfService : Int {
case userInteractive 最高优先级,用于用户交互事件
case userInitiated 次高优先级,用于用户需要马上执行的事件
//默认优先级,主线程和没有设置优先级的线程都默认为这个优先级
case `default`
case utility 普通优先级,用于普通任务
case background 最低优先级,用于不重要的任务
}
let thread1 = Thread(target: self, selector: #selector(method1), object: nil)
let thread2 = Thread(target: self, selector: #selector(method2), object: nil)
thread1.qualityOfService = .userInteractive
thread2.qualityOfService = .background
thread1.start()
thread2.start()
@objc func method1() {
for _ in 0...1 {
NSLog("11111--\(Thread.current)")
}
}
@objc func method2() {
for _ in 0...1 {
NSLog("22222--\(Thread.current)")
}
}
打印结果: 11111--<NSThread: 0x6000011287c0>{number = 5, name = (null)}
11111--<NSThread: 0x6000011287c0>{number = 5, name = (null)}
22222--<NSThread: 0x600001128a40>{number = 6, name = (null)}
22222--<NSThread: 0x600001128a40>{number = 6, name = (null)}
服务优先级高的任务能优先获得更多的资源
7. 线程睡眠,使线程处于等待状态
let thread1 = Thread(target: self, selector: #selector(method1), object: nil)
thread1.start()
@objc func method1() {
for i in 0...3 {
NSLog("11111--\(Thread.current)")
if i == 1 {
Thread.sleep(forTimeInterval: 5.0)
}
}
}
打印结果:
2020-04-10 17:46:06 11111--<NSThread: 0x600002e5f380>{number = 5, name = (null)}
2020-04-10 17:46:06 11111--<NSThread: 0x600002e5f380>{number = 5, name = (null)}
2020-04-10 17:46:11 WGFcodeNotes[13188:379557] 11111--<NSThread:
0x600002e5f380>{number = 5, name = (null)}
2020-04-10 17:46:11 WGFcodeNotes[13188:379557] 11111--<NSThread:
0x600002e5f380>{number = 5, name = (null)}
8.线程退出
let thread1 = Thread(target: self, selector: #selector(method1), object: nil)
thread1.start()
@objc func method1() {
for i in 0...3 {
NSLog("11111--\(Thread.current)")
if i == 1 {
Thread.exit()
}
}
NSLog("方法1执行完成了")
}
打印结果: 11111--<NSThread: 0x600002436900>{number = 5, name = (null)}
11111--<NSThread: 0x600002436900>{number = 5, name = (null)}