zoukankan      html  css  js  c++  java
  • ios之runloop笔记

    网上关于runloop的文章不计其数,再此,贴个自认为讲的比较简单明了的文章

    个人理解:

    ios的runloop应该是类似于线程之间的消息监听+队列(队列于外部不透明,支持多重send消息模式,perform selector,timer,UI事件等等)
    和android的Looper非常相似,和windows的消息循环也很类似,具体底层实现不关注,直接贴测试代码
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property(nonatomic, strong) NSThread *thread;
    @property(nonatomic, strong) NSThread *msgThread;
    
    @end
    
    @implementation ViewController
    
    - (void) viewDidLoad{
        [super viewDidLoad];
        
        [self initMsgThread];
    }
    
    - (void) initMsgThread{
        
        self.msgThread = [[NSThread alloc]initWithTarget:self selector:@selector(msgThreadInitFunc) object:nil];
        [self.msgThread start];
    }
    
    - (void) msgThreadInitFunc{
        NSLog(@"msgThreadInitFunc run, %@",[NSThread currentThread]);
        
        [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
        
        NSLog(@"msgThreadInitFunc run");
        
    }
    
    //******************************************************************************************************************
    - (void) postMsgToThread{
        [[[NSThread alloc]initWithTarget:self selector:@selector(asyncPostMsgToThread) object:nil] start];
    }
    
    - (void) asyncPostMsgToThread{
        [self performSelector:@selector(onThreadMsgProc) onThread:self.msgThread withObject:nil waitUntilDone:NO];
    }
    
    - (void) onThreadMsgProc{
        NSLog(@"do some work %@, %s", [NSThread currentThread], __FUNCTION__);
    }
    
    //******************************************************************************************************************
    - (void) startTimerOnThread{
        self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(asyncStartTimerOnThread) object:nil];
        [self.thread start];
    }
    
    - (void) asyncStartTimerOnThread{
        [self performSelector:@selector(asyncStartTimer) onThread:self.thread withObject:nil waitUntilDone:NO];
        
    //    [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    }
    
    - (void) asyncStartTimer{
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerFired:)userInfo:nil repeats:YES];
    }
    
    - (void) timerFired:(id)timer{
        NSLog(@"on thread timer %s",__FUNCTION__);
    }
    
    //******************************************************************************************************************
    - (IBAction)testButtonTapped:(id)sender {
    //    [self postMsgToThread];
        [self startTimerOnThread];
    }
    
    @end

    当然用block也是一样的,子线程必须创建runloop来监听消息,否则这个子线程是无法处理类似performSelector,NSTimer之类的消息的

    线程之间通信,cocos2dx,u3d,ios,android,win32,都是基于消息队列的模式,一个发,一个收,写时加锁,别无更好的办法了

  • 相关阅读:
    JqueryAjax 常用复制
    linux 下调用wps 注意
    java 里执行javascript代码
    JS学习笔记
    javascript 区域外事件捕捉setCapture
    【MySQL基础打卡(一)】查询语句项目作业
    【分类问题中模型的性能度量(二)】超强整理,超详细解析,一文彻底搞懂ROC、AUC
    【分类问题中模型的性能度量(一)】错误率、精度、查准率、查全率、F1详细讲解
    【机器学习实战学习笔记(1-2)】k-近邻算法应用实例python代码
    【机器学习实战学习笔记(1-1)】k-近邻算法原理及python实现
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3180904.html
Copyright © 2011-2022 走看看