美文网首页
iOS 线程保活

iOS 线程保活

作者: 无名指的情怀 | 来源:发表于2020-05-28 16:13 被阅读0次

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

//

//  ZFPermenantThread.h

//  ZFThread

//

//  Created by zzf on 2020/5/26.

//  Copyright © 2020 zzf. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface ZFPermenantThread : NSObject

- (void)run;

- (void)stop;

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

@end

NS_ASSUME_NONNULL_END

2、线程保活管理类.m文件

//

//  ZFPermenantThread.m

//  ZFThread

//

//  Created by zzf on 2020/5/26.

//  Copyright © 2020 zzf. All rights reserved.

//

#import "ZFPermenantThread.h"

@interface ZFThread : NSThread

@end

@implementation ZFThread

- (void)dealloc {

    NSLog(@"%s",__func__);

}

@end

@interface ZFPermenantThread ()

@property (nonatomic, strong)ZFThread *innerThread;

@property (nonatomic, assign, getter=isStopped)BOOL stopped;

@end

@implementation ZFPermenantThread

- (instancetype)init {

    if(self= [superinit]) {

        self.stopped=NO;

        __weaktypeof(self) weakSelf =self;

        self.innerThread= [[ZFThreadalloc]initWithBlock:^{

            [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];

            while(weakSelf && !weakSelf.isStopped) {

                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

            }

        }];

        [self.innerThreadstart];

    }

    return self;

}

- (void)run{

    if(!self.innerThread) {

        return;

    }

    [self.innerThread start];

}

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

    if(!self.innerThread|| !task) {

        return;

    }

    [self performSelector:@selector(__executeTask:) onThread:self.innerThread withObject:task waitUntilDone:NO];

}

- (void)stop{

    if(!self.innerThread) {

        return;

    }

    [self performSelector:@selector(__stop) onThread:self.innerThread withObject:nil waitUntilDone:YES];

}

- (void)dealloc {

    NSLog(@"%s",__func__);

    [selfstop];

}

#pragma mark - private methods

- (void)__stop{

    self.stopped=YES;

    CFRunLoopStop(CFRunLoopGetCurrent());

    self.innerThread = nil;

}

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

    task();

}

@end

3、使用调用

//

//  ViewController.m

//  ZFThread

//

//  Created by zzf on 2020/5/26.

//  Copyright © 2020 zzf. All rights reserved.

//

#import "ViewController.h"

#import "ZFPermenantThread.h"

@interface ViewController ()

@property (nonatomic, strong)ZFPermenantThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.thread = [[ZFPermenantThread alloc] init];

//    [self.thread run];

}

- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event {

    [self.thread executeTask:^{

        NSLog(@"执行任务");

    }];

}

- (void)test{

}

- (void)stop{

    [self.threadstop];

}

- (void)dealloc {

//    [self.thread stop];

}

@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/uwsaahtx.html