iOS NTThread 与 NSRunLoop 使用

使用

示例代码,通过创建 OMTThread 实例,并开启线程。使用 NSTimer 每 3 秒执行一次方法。

#import "ThreadViewController.h"
#import "OMTThread.h"


static NSString *const kThreadName = @"org.yzr.thread";

@interface ThreadViewController ()

@property (nonatomic, strong) OMTThread *thread;

@end

@implementation ThreadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.thread start];
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(excute) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)excute {
    [self callBlock:^{
        NSLog(@"thread name:%@", [NSThread currentThread].name);
        NSLog(@"我在执行方法");
    } onThread:self.thread];
}

- (OMTThread *)thread {
    if (!_thread) {
        _thread = [[OMTThread alloc] initWithName:kThreadName qos:NSQualityOfServiceUserInteractive];
    }
    return _thread;
}
@end

OMTThread.h

#import <Foundation/Foundation.h>

@interface OMTThread : NSThread
/// 初始化
- (instancetype)initWithName:(NSString *)name qos:(NSQualityOfService)qos;

@end

@interface NSObject (OMTThreadPerformAdditions)
/// 在指定线程中执行 block
- (void)callBlock:(dispatch_block_t)block onThread:(NSThread *)thread;

@end

OMTThread.m

#import "OMTThread.h"
#import <pthread.h>

@implementation OMTThread

- (instancetype)initWithName:(NSString *)name qos:(NSQualityOfService)qos {
    if (self = [super initWithTarget:self.class selector:@selector(runRunLoop) object:nil]) {
        self.qualityOfService = qos;
        self.name = name;
    }
    return self;
}

+ (void)runRunLoop {
    @autoreleasepool {
        // 设置线程名字
        pthread_setname_np([NSThread currentThread].name.UTF8String);
        
        // 设置虚拟的 runloop 源,避免旋转
        CFRunLoopSourceContext noSpinCtx = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
        CFRunLoopSourceRef noSpinSource = CFRunLoopSourceCreate(NULL, 0, &noSpinCtx);
        CFRunLoopAddSource(CFRunLoopGetCurrent(), noSpinSource, kCFRunLoopDefaultMode);
        CFRelease(noSpinSource);
        
        // 运行 RunLoop
        while (kCFRunLoopRunStopped != CFRunLoopRunInMode(kCFRunLoopDefaultMode, ((NSDate *)[NSDate distantFuture]).timeIntervalSinceReferenceDate, NO)) {
            NSLog(@"退出 Runloop");
        }
    }
}

@end

@implementation NSObject (OMTThreadPerformAdditions)

- (void)callBlock:(dispatch_block_t)block onThread:(NSThread *)thread {
    [self performSelector:@selector(_excuteBlock:) onThread:thread withObject:block waitUntilDone:NO];
}

- (void)_excuteBlock:(dispatch_block_t)block {
    if (block) {
        block();
    }
}

@end
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装、继承、多态。它既具有静态语言的特性...
    克鲁德李阅读 3,273评论 0 0
  • 文/Jack_lin(简书作者)原文链接:http://www.jianshu.com/p/5d2163640e2...
    笔笔请求阅读 3,685评论 0 0
  • 来自网络 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了。今年,找过工作人可能会更深刻地体会...
    用心在飞阅读 4,330评论 5 4
  • OC的理解与特性OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装、继承、多态。它既具有静态语言的特性(...
    LIANMING_LI阅读 3,529评论 0 0
  • OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装、继承、多态。它既具有静态语言的特性...
    小楼昨夜有风雨阅读 3,660评论 0 0

友情链接更多精彩内容