Runloop源码 https://opensource.apple.com/tarballs/CF/
Runloop与线程
- 每条线程都有唯一的一个与之对应的Runloop对象
- Runloop保存在一个全局的Dictionary里,线程为key,runloop为value
- 线程刚创建时并没有runloop对象,runloop会在第一次获取它时创建
- Runloop会在线程结束时销毁
Runloop对象结构
Runloop几种状态
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
kCFRunLoopEntry = (1UL << 0),
kCFRunLoopBeforeTimers = (1UL << 1),
kCFRunLoopBeforeSources = (1UL << 2),
kCFRunLoopBeforeWaiting = (1UL << 5),
kCFRunLoopAfterWaiting = (1UL << 6),
kCFRunLoopExit = (1UL << 7),
kCFRunLoopAllActivities = 0x0FFFFFFFU
};
Runloop应用
- 控制线程生命周期(线程保活)
- 解决NSTimer在滑动时停止工作的问题
- 监控应用卡顿
- 性能优化