美文网首页多线程iOS底层
iOS 子线程用runloop保活的一个方案

iOS 子线程用runloop保活的一个方案

作者: Sweet丶 | 来源:发表于2020-10-24 21:58 被阅读0次

在我们需要频繁地开启子线程执行操作的时候,我们可以采用开启子线程runloop的方式保活子线程,这样避免频繁创建线程销毁线程的开销。
具体的保活方式.

@interface ZLPermanentThread : NSObject
/** 在当前子线程执行一个任务*/
- (void)executeTask:(void(^)(void))task;
/**结束线程*/
- (void)stop;
@end

@interface ZLPermanentThread()
@property (nonatomic, strong) NSThread *thread;
//@property (nonatomic, assign, getter=isStopped) BOOL stopped;
@end

@implementation ZLPermanentThread

- (instancetype)init{
    if (self = [super init]) {
//        self.stopped = NO;
//        __weak typeof(self) weakSelf = self;
        self.thread = [[NSThread alloc] initWithBlock:^{// ios 10之后才有这个方法
            
            CFRunLoopSourceContext context = {0};
            CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
            CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
            CFRelease(source);
            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, false);

            NSLog(@"runloop end---");
//            NSRunLoop *currentRunloop = [NSRunLoop currentRunLoop];
//            [currentRunloop addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];
//            while (weakSelf && !weakSelf.isStopped) {
//                [currentRunloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//            }
        }];
        [self.thread start];
    }
    return self;
}

- (void)__executeTask:(void (^)(void))task{
    task();
}

- (void)executeTask:(void (^)(void))task{
    if (!self.thread || !task) return;
    
    [self performSelector:@selector(__executeTask:) onThread:self.thread withObject:task waitUntilDone:NO];
}

- (void)stop{
    if (!self.thread) return;
    
    [self performSelector:@selector(__stop) onThread:self.thread withObject:nil waitUntilDone:YES];
}

- (void)__stop{
//    self.stopped = YES;
    CFRunLoopStop(CFRunLoopGetCurrent());
    self.thread = nil;
}

- (void)dealloc{
    NSLog(@"%s", __func__);
    [self stop];
}

@end

使用时

// 初始化
self.thread = [[ZLPermanentThread alloc] init];
// 执行任务
[self.thread executeTask:^{
    NSLog(@"子线程执行了任务");
}];

关键点:

  1. 子线程的runloop需要手动去调用运行。
  2. 子线程runloop手动运行成功开启循环需要先在runloop中添加事件源或者timer。
  3. CFRunLoopStop(CFRunLoopGetCurrent());可以停止runloop运行。
附:Runloop相关知识点
1. Runloop与线程的关系?

Runloop是一个运行循环,与线程是一一对应的关系,线程中有runloop或者自己定义的循环运行时,子线程就不会被销毁,主线程中Runloop是默认开启的;runloop是系统提供的运行循环,相比自己手动写while循环,有以下几个优点:

  1. 能处理APP的input souce事件、定时器事件、代码执行的事件。
  2. 在没有事件要处理时会进入休眠(系统层面的休眠函数),节省CPU资源。

下面的runloop的本质结构:

// 下载最大版本号的源码压缩包,找到__CFRunLoop.
// 下面是简化了一些变量后的__CFRunLoop
struct __CFRunLoop {
    pthread_t _pthread;
    CFMutableSetRef _commonModes; // 共同的模式,相当于NSRunLoopCommonModes
    CFMutableSetRef _commonModeItems;// 共同的模式下的Item
    CFRunLoopModeRef _currentMode;
    CFMutableSetRef _modes;
};
// 下面是简化了一些变量后的__CFRunLoopMode
struct __CFRunLoopMode {
    CFStringRef _name;
    CFMutableSetRef _sources0;
    CFMutableSetRef _sources1;
    CFMutableArrayRef _observers;
    CFMutableArrayRef _timers;
};

有几件事情需要说明一下:

1.runloop同一时间只能处于一个RunLoopMode下.

  1. 系统提供每个RunLoopMode下会有自己的Item,这个item可能是source,timer,observer; 自定义的RunLoopMode,自定义的RunLoopMode需要自己往里面添加Item。
  2. 如果一个runloop中没有任何一个item,那么runloop会立即停止。
2. Runloop有几种模式?

官网显示的是5种,我们需要关心的就是:Default模式、tracking模式、Common模式。NSRunloop简单细说(六)—— 几种循环模式详细解析

3. Runloop有几种状态?

7种状态:即将进入、即将处理timer、即将处理source、即将进入休眠、从休眠中唤醒、退出runloop、AllActivities。
/* Run Loop Observer Activities */
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
};

4. Runloop是怎么运行的

Runloop运行的时候是开启了一个do..while循环,执行一个循环时首先处理完Observer、Source0、Source1、timer,然后调用系统的休眠函数等待被唤醒,最后在唤醒时继续处理事件再次进入循环。 Runloop的内部结构与运行原理

5. Runloop的一些运用?
  • 使用NSTimer、CADisplayLink时需要addTimer:forMode:, 这个时候如果不希望滚动视图时停止timer就需要传入NSRunLoopCommonModes.
  • 在子线程中使用- (void)performSelector: withObject:afterDelay:时方法不会得到执行,原因就是子线程runloop未开启而这个方法的实现是开启timer将timer加到了runloop。
  • 常驻子线程见上面的实现。
  • iOS系统对主线程Runloop的状态进行了监听addObserver:,在runloop开启时创建了一个autoreleasepool,在进入休眠时销毁这个autoreleasepool然后创建一个新的autoreleasepool,在runloop退出时销毁autoreleasepool,这样完成了自动的对对象内存的管理。

推荐阅读:
博客:NSRunloop简单细说(六)—— 几种循环模式详细解析
官方文档:Runloop官方文档
源码:CF框架源码

相关文章

  • iOS底层原理——浅谈RunLoop

    RunLoop应用:线程保活 线程保活、控制销毁 iOS-浅谈RunLoop8iOS底层原理总结 - RunLoo...

  • iOS 子线程用runloop保活的一个方案

    在我们需要频繁地开启子线程执行操作的时候,我们可以采用开启子线程runloop的方式保活子线程,这样避免频繁创建线...

  • iOS Runloop的理解与使用

    Runloop的概念 Runloop的存在主要就是为了线程保活,线程保活是为了线程能够及时的处理事件,不会在其执行...

  • iOS Runloop 线程保活及坑

    iOS Runloop 线程保活及坑 上面的代码, Thread 这个类继承自 NSThread ,然后重写了 d...

  • iOS总结篇-RunLoop

    RunLoop是什么? RunLoop是一个事件循环,让线程休眠和线程保活成为了可能,线程休眠可以节省CPU资源;...

  • iOS笔记-runloop

    runloop可以做什么? 处理crash 保持线程存活(线程保活) 监测、优化卡顿 线程和runloop有什么关...

  • RunLoop 02 - 应用(线程保活)

    RunLoop 02 - 应用(线程保活) PermanentThread PermanentThread 使用示例

  • RunLoop学习笔记

    参考深入理解RunLoop深入研究 Runloop 与线程保活RunLoop分享by孙源 RunLoop的概念 R...

  • 10.3 runloop 的实际应用

    runloop实际中的应用 控制线程生命周期(线程保活)、崩溃的起死回生 runloop和performselec...

  • Runloop源码解析:运行逻辑

    Runloop应用: Timer失效问题; 线程保活; Runloop的运行逻辑: 入口函数 通知Observer...

网友评论

    本文标题:iOS 子线程用runloop保活的一个方案

    本文链接:https://www.haomeiwen.com/subject/rlhwmktx.html