iOS NTThread 與 NSRunLoop 使用

使用

示例代碼,通過(guò)創(chuàng)建 OMTThread 實(shí)例,并開(kāi)啟線程。使用 NSTimer 每 3 秒執(zhí)行一次方法。

#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(@"我在執(zhí)行方法");
    } 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)
/// 在指定線程中執(zhí)行 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 {
        // 設(shè)置線程名字
        pthread_setname_np([NSThread currentThread].name.UTF8String);
        
        // 設(shè)置虛擬的 runloop 源,避免旋轉(zhuǎn)
        CFRunLoopSourceContext noSpinCtx = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
        CFRunLoopSourceRef noSpinSource = CFRunLoopSourceCreate(NULL, 0, &noSpinCtx);
        CFRunLoopAddSource(CFRunLoopGetCurrent(), noSpinSource, kCFRunLoopDefaultMode);
        CFRelease(noSpinSource);
        
        // 運(yùn)行 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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • OC的理解與特性 OC作為一門(mén)面向?qū)ο蟮恼Z(yǔ)言,自然具有面向?qū)ο蟮恼Z(yǔ)言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語(yǔ)言的特性...
    克魯?shù)吕?/span>閱讀 501評(píng)論 0 0
  • 文/Jack_lin(簡(jiǎn)書(shū)作者)原文鏈接:http://www.itdecent.cn/p/5d2163640e2...
    筆筆請(qǐng)求閱讀 627評(píng)論 0 0
  • 來(lái)自網(wǎng)絡(luò) 序言 目前形勢(shì),參加到iOS隊(duì)伍的人是越來(lái)越多,甚至已經(jīng)到供過(guò)于求了。今年,找過(guò)工作人可能會(huì)更深刻地體會(huì)...
    用心在飛閱讀 920評(píng)論 5 4
  • OC的理解與特性O(shè)C作為一門(mén)面向?qū)ο蟮恼Z(yǔ)言,自然具有面向?qū)ο蟮恼Z(yǔ)言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語(yǔ)言的特性(...
    LIANMING_LI閱讀 575評(píng)論 0 0
  • OC的理解與特性 OC作為一門(mén)面向?qū)ο蟮恼Z(yǔ)言,自然具有面向?qū)ο蟮恼Z(yǔ)言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語(yǔ)言的特性...
    小樓昨夜有風(fēng)雨閱讀 617評(píng)論 0 0

友情鏈接更多精彩內(nèi)容