网上关于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. js组件的写法

    工作之中的不足,报了js培训班,因为工作加班原因缺了几天课(js组件开发),现在拾起来补补 <!doctype html> <html> <head> <me ...

  2. 【转载】oracle索引详解2

    原文URL:http://justplayoop1.iteye.com/blog/1259562 一. 索引介绍 1.1  索引的创建 语法 : CREATE UNIUQE | BITMAP INDE ...

  3. L1-027 出租

    下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对 ...

  4. bacula配置

    Bacula Bacula是一款开源的跨平台网络备份工具,提供基于企业级的CS的备份解决方案.可以对数据进行备份.恢复.以及完整性校验.  功能特点: 支持完全备份,增量备份,差异备份. 支持多种恢复 ...

  5. STL标准库-容器适配器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 上一节介绍了仿函数适配器,这节主要介绍容器适配器和迭代器适配器的概念,其实容器适配器和迭代器其适配器就是封装了一些其他class ...

  6. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  7. URL的应用

    1.对于Android来说,开发应用都会去访问服务器地址,那么就要连网,需要通过URL. 先new一个URL来获取路径,然后利用HttpURLConnection来连接并打开url,并通过get 请求 ...

  8. liunx的磁盘管理的基本命令

    df     查看磁盘占用率 du -sh    查看磁盘多大 sudo fdisk -l   查看硬盘信息 sudo mkfs -t ext3 /dev/sdb1    建立文件系统(相当于格式化) ...

  9. [LeetCode&Python] Problem 557. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  10. js中准确判断数据类型的方法

    一   通用的typeof 方法 typeof  ture    输出   Boolean typeof  123   输出     number ..... 但是   typeof 无法判断  nu ...