网上关于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,都是基于消息队列的模式,一个发,一个收,写时加锁,别无更好的办法了

ios之runloop笔记的更多相关文章

  1. iOS 字符串处理笔记

    iOS字符串处理笔记,包括如何使用正则表达式解析,NSScanner扫描,设置和使用CoreParse解析器来解析处理自定义符号等内容 搜索 在一个字符串中搜索子字符串 最灵活的方法 - (NSRan ...

  2. iOS多线程-RunLoop简介

    什么是RunLoop? 从字面上来看是运行循环的意思. 内部就是一个do{}while循环,在这个循环里内部不断的处理各种任务(比如:source/timer/Observer) RunLoop的存在 ...

  3. RunLoop笔记

    1.Runloop基础知识 - 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如 ...

  4. iOS - OC RunLoop 运行循环/消息循环

    1.RunLoop 1)运行循环: 运行循环在 iOS 开发中几乎不用,但是概念的理解却非常重要. 同一个方法中的代码一般都在同一个运行循环中执行,运行循环监听 UI 界面的修改事件,待本次运行循环结 ...

  5. IOS懒人笔记应用源码

    这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...

  6. iOS关于RunLoop和Timer

    RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在.RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop.永远不要手动创建一个run ...

  7. iOS开发RunLoop

    最近处于离职状态,时间也多了起来,但是学习还是不能放松,今天总结一下RunLoop,RunLoop属于iOS系统层的东西,还是比较重要的. 一.什么是RunLoop 字面意思看是跑圈,也可以看作运行循 ...

  8. iOS之RunLoop

    RunLoop是iOS线程相关的比较重要的一个概念,无论是主线程还是子线程,都对应一个RunLoop,如果没有RunLoop,线程会马上被系统回收. 本文主要CFRunLoop的源码解析,并简单阐述一 ...

  9. iOS开发-Runloop详解(简书)

    不知道大家有没有想过这个问题,一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件 ...

随机推荐

  1. 014PHP文件处理——文件指针控制fseek rewind ftell feof fpassthru

    <?php /** * 文件指针控制fseek rewind ftell feof fpassthru */ //feof()判断文件读取是否超出文件长度 /*$file = fopen('a. ...

  2. POJ 1062 最短路Dijstra

    汉语题... 题意正如你看到的酱... 看的解题报告.思路大概是把每个点看做最高等级.然后枚举所有当前可以访问的点.进行dijstra算法.找到此时到目标点最短路.枚举完之后找到最小的点就可以了. P ...

  3. bzoj4001

    题解: 答案就是n*(n+1)/2/(2*n-1) 代码: #include<bits/stdc++.h> double n; int main() { scanf("%lf&q ...

  4. 关于list集合存储null的问题

    工作中,遇到list集合存储null的问题,不确定list能否存储null值.于是写一些demo测试list,set,table,及map存储null的问题. 1.list之arraylist pub ...

  5. flask+APScheduler 任务调度,计划任务,定时任务

    from flask import Flask from flask_apscheduler import APScheduler # 引入APScheduler from test124 impor ...

  6. PHP 之 Ci框架下隐藏index.php

    1. 修改 apache 配置文件 开启重写模块 conf/httpd.conf 去掉前面的# LoadModule rewrite_module modules/mod_rewrite.so 对于U ...

  7. SWIFT中调用Segue的几个方法

    场景1: 如图所示,在视图的第一个按钮处拉出一条Segue到另外一个视图,并给这个Segue命名,如SegueOne 此时可以用代码调用这个Segue切换视图: self.performSegueWi ...

  8. HDU 1254 推箱子(BFS)

    Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...

  9. 框架流程图绘制工具OmniGraffle 7 for Mac

    1.官网下载地址 https://www.omnigroup.com/omnigraffle 2.激活方法 Omnigraffle Pro 7注册码/许可证 名字:Appked 序列号:MFWG-GH ...

  10. 开源库dlib的安装与编译-CMake

    前言 最近项目涉及到关于face alignment的实现,了解到目前主要的算法有ERT.SDM.LBF等,其中由于dlib开源库实现了ERT算法,效果也很不错,故开始研究dlib的使用.而使用的第一 ...