美文网首页
iOS:线程保活

iOS:线程保活

作者: 春暖花已开 | 来源:发表于2020-08-05 20:27 被阅读0次
自定义子线程MZChildThread
//MZChildThread.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MZChildThread : NSObject

///在子线程中执行任务
- (void)executeTask:(void(^)(void))task;

///停止并销毁线程
- (void)stopThread;

@end

NS_ASSUME_NONNULL_END

//MZChildThread.m
#import "MZChildThread.h"

#if DEBUG
#ifndef MZPrivateThreadLog
#define MZPrivateThreadLog(...) NSLog(__VA_ARGS__)
#else
#define MZPrivateThreadLog(...)
#endif
#endif

#pragma mark - _MZPrivateThread
@interface MZPrivateThread : NSThread

@end

@implementation MZPrivateThread

- (void)dealloc {
    MZPrivateThreadLog(@"%s", __func__);
}

@end


#pragma mark - MZChildThread

@interface MZChildThread ()

@property (nonatomic, strong) MZPrivateThread *thread;
@property (nonatomic, assign) BOOL shouldStop;

@end

@implementation MZChildThread

#pragma mark - public Methods

- (instancetype)init {
    if (self = [super init]) {

        __weak typeof(self) weakSelf = self;
        self.shouldStop = NO;

        self.thread = [[MZPrivateThread alloc] initWithBlock:^{
            //将端口作为输入源添加到运行循环的指定模式
            [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];

            while (weakSelf && !weakSelf.shouldStop) {
                //相当于run,但又与run不同的是,可以自己控制退出时机
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }

            //当线程保活时,该打印将被阻塞
            MZPrivateThreadLog(@"===end===");
        }];

        //开启线程
        [self.thread start];
    }
    return self;
}

- (void)executeTask:(void (^)(void))task {

    if (!self.thread || !task) return;

    [self performSelector:@selector(p_executeTask:) onThread:self.thread withObject:task waitUntilDone:NO];
}

- (void)stopThread {

    if (!self.thread) return;

    //waitUntilDone为YES,表示执行完子线程代码后,才执行之后的代码
    [self performSelector:@selector(p_stopThread) onThread:self.thread withObject:nil waitUntilDone:YES];
}


#pragma mark - public Methods

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

- (void)p_stopThread {
    self.shouldStop = YES;

    //停止当前线程(一定要在开启的子线程中关闭),之后会解除阻塞
    CFRunLoopStop(CFRunLoopGetCurrent());
    self.thread = nil;
}

- (void)dealloc {
    //在释放的时候,要关闭线程,防止占用资源
    [self stopThread];
    MZPrivateThreadLog(@"%s", __func__);
}

@end
使用
#import "DetailViewController.h"

#import "MZChildThread.h"

@interface DetailViewController ()

@property (nonatomic, strong) MZChildThread *thread;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.thread = [[MZChildThread alloc] init];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.thread executeTask:^{
        NSLog(@"执行任务在 - %@", [NSThread currentThread]);
    }];
}

- (IBAction)stop {
    [self.thread stopThread];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end

相关文章

  • iOS底层原理——浅谈RunLoop

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

  • iOS NSThread 保活线程代码封装

    iOS NSThread 保活线程代码封装

  • iOS 线程保活

    [self performSelectorInBackground:@selector(dealInsertMo...

  • iOS 线程保活

    1、线程保活管理类.h文件 // // ZFPermenantThread.h // ZFThread // //...

  • iOS线程保活

    简介 大家好!我是Tony,一个热爱技术,希望运用技术改变生活的的追梦男孩。闲话不多说,今天聊聊iOS的线程保活。...

  • iOS:线程保活

    自定义子线程MZChildThread 使用

  • iOS 线程保活

    开发中,经常会遇到将耗时操作放到子线程中执行,来提升应用性能的场景。当子线程中的任务执行完毕后,线程就被立刻销毁。...

  • iOS线程保活

    一.什么是线程保活 如图1所以,任务执行完成后,线程会退出。线程的创建和销毁比较耗性能,如果需要在一条线程中频繁的...

  • iOS 线程保活

    JXPermenantThread 子线程保活: 快速创建子线程,让子线程一直存活,并提供执行任务的block,直...

  • iOS 题目详解 部分二

    主要讲解子线程的保活方式, 以及 Autorelease 对象的释放时机 iOS 题目详解 部分一iOS 题目...

网友评论

      本文标题:iOS:线程保活

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