IOS RunLoop 常驻线程的实现
线程常驻,正如其名,我们要实现的事让一个线程长期存在,不被销毁。
这时会有人说,那还不简单吗。
但是这里我们要实现的事如何让线程座椅待命,而且并不是主线程。
首先介绍一下正常情况下的线程使用。
//
// ViewController.m
// CX RunLoop 常驻线程的实现
//
// Created by ma c on 16/3/30.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXThread.h"
@interface ViewController () @property (nonatomic, strong)CXThread * thread; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; NSThread* thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil]; [thread start]; }
-(void)run{ NSLog(@"run -- 旭宝爱吃鱼"); }
-(void)test{ NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]); }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self test]; //让test方法在线程thread上实现
// [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil]; }
@end
上面的代码知识简单的实现了线程的使用。
下面是其效果图(注意线程的销毁)
实际上test与thread并没有关系。
我知识简单的让其输出默认的主线程日志,以供后面对比。
下面是让thread为全局变量
//
// ViewController.m
// CX RunLoop 常驻线程的实现
//
// Created by ma c on 16/3/30.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXThread.h"
@interface ViewController () @property (nonatomic, strong)CXThread * thread; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil]; [_thread start]; }
-(void)run{ NSLog(@"run -- 旭宝爱吃鱼"); }
-(void)test{ NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]); }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self test]; //让test方法在线程thread上实现
// [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil]; }
@end
由效果图我们可以发现。thread并没有销毁。而且test,依旧是在主线程上实现的。
但我们想要的是test在thread上实现(实际开发中是不允许耗时操作在主线程中的)
我们让test在thread中实现:(注意虾米那方法并不成功)
//
// ViewController.m
// CX RunLoop 常驻线程的实现
//
// Created by ma c on 16/3/30.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXThread.h"
@interface ViewController () @property (nonatomic, strong)CXThread * thread; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil]; [_thread start]; }
-(void)run{ NSLog(@"run -- 旭宝爱吃鱼"); }
-(void)test{ NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]); }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 让test方法在线程thread上实现
[self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:YES]; }
@end
为什么会不成功呢??(我真的点击了)
原因是我们只是单纯的建立了一个线程。。。很单纯的。。。考虑一下我们该怎么做。
那么我们有两种做法实现。
方法一(比较正常的方法)
//
// ViewController.m
// CX RunLoop 常驻线程的实现
//
// Created by ma c on 16/3/30.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXThread.h"
@interface ViewController () @property (nonatomic, strong)CXThread * thread; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil]; [_thread start]; }
-(void)run{ NSLog(@"run -- 旭宝爱吃鱼");
//添加Port 实时监听
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//添加runloop
[[NSRunLoop currentRunLoop]run]; }
-(void)test{ NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]); }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 让test方法在线程thread上实现
[self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO]; }
@end
就是这么简单。
方法二
//
// ViewController.m
// CX RunLoop 常驻线程的实现
//
// Created by ma c on 16/3/30.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXThread.h"
@interface ViewController () @property (nonatomic, strong)CXThread * thread; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil]; [_thread start]; }
-(void)run{ NSLog(@"run -- 旭宝爱吃鱼"); while () {
//添加runloop
[[NSRunLoop currentRunLoop]run];
}
}
-(void)test{ NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]); }
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 让test方法在线程thread上实现
[self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO]; }
@end
IOS RunLoop 常驻线程的实现的更多相关文章
- iOS之创建一个常驻线程
// 当创建一个线程,并且希望它一直存在时,但往往我们创建的线程都是执行完成之后也就停止了,不能再次利用,那么如何创建一个线程可以让他可以再次工作呢,这个时候就需要使用到RunLoop了.下面的是我写 ...
- iOS Runloop理解
一.RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心 ...
- IOS多线程之线程的创建
版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前也说过线程是消耗资源的.多线程会占用你应用程序(和系统的)的内存使用和性能方面的资源.我们创建一个线程后可以对他的一些部分进行配置例如可以对 ...
- iOS RunLoop详解
1. RunLoop简介 1.1 什么是RUnLoop 可以理解为字面的意思:Run表示运行,Loop表示循环.结合在一起就是运行的循环.通常叫做运行循环. RunLoop实际上是一个对象,这个对象在 ...
- iOS Runloop 消息循环
介绍 Runloop是一种事件监听循环,可以理解成一个while死循环,监听到事件就起来,没有就休息. Runloop可以在不同模式下进行切换,iOS有五种模式,其中UIInitializationR ...
- iOS runLoop 原理多线程 总结 NSTimer优化
可以理解为字面意思:Run 表示运行,Loop 表示循环.结合在一起就是运行的循环的意思.哈哈,我更愿意翻译为『跑圈』.直观理解就像是不停的跑圈. RunLoop 实际上是一个对象,这个对象在循环中用 ...
- IOS RunLoop面试题
一 什么是RunLoop? 从字面意思看就是运行循环,其实内部就是do-while循环,这个循环内部不断地处理各种任务(比 如Source,Timer,Observer) 一个线程对应一个RunLoo ...
- ios -RunLoop(简单理解)
一. RunLoop简介 RunLoop字面意思是运行时,即跑圈得意思.它可以在我们需要的时候自己跑起来运行,在我们没有操作的时候就停下来休息,充分节省CPU资源,提高程序性能. 二. RunLoop ...
- ios runloop学习
今天突然才之间才意识到NSTimer这样的运行方式,是在多线程中实现的循环还是在主线程中去实现的呢.当然不可能是在主线程中的while那么简单,那样什么都干不了,简单看了下NSTimer是以同步方式运 ...
随机推荐
- JavaSE高级之GUI编程
下面主要用到了java中的swing进行界面设计,当然java的GUI不如C#的设计的好看,不过原理还是要会的. 1. GUI Graphical User Interface 用户图形界面 a) 主 ...
- 探秘Tomcat——启动篇
tomcat作为一款web服务器本身很复杂,代码量也很大,但是模块化很强,最核心的模块还是连接器Connector和容器Container.具体请看下图: 从图中可以看出 a. 高亮的两块是Conne ...
- Kafka到Hdfs的数据Pipeline整理
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 找时间总结整理了下数据从Kafka到Hdfs的一些pipeline,如下 1> Kafka ...
- 测试为什么Low
你从来没有因为一个歌手不会写曲填词而说歌手很Low! 你从来没有因为一个演员不会摄影.唱歌而说演员很Low! 你从来没有因为一个记者不会摄影,拍照而说记者很Low! 你从来没有因为一个美食家不会烧菜, ...
- HTML基本元素(一)
HTML基本元素(一) 1.换行符 <br /> Ps:br 是换行(Break)的缩写,文本会在这个标签的地方换行. 实例: 第一行<br />第二行 2.段落 <p& ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
- 史上最全的Ajax基础详解
同步请求和异步请求 先解释一下同步和异步的概念: 同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 异步是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通讯 ...
- Effective java笔记(四),泛型
泛型为集合提供了编译时类型检查. 23.不要在代码中使用原生态类型 声明中具有一个或多个类型参数的类或接口统称为泛型.List<E>是一个参数化类,表示元素类型为E的列表.为了提供兼容性, ...
- failover机制的小讨论
对于一个7*24小时无间断的线上服务来说,在服役时间内难免会遇到一些fail,例如db断开连接且短暂连接不上了, 下游的某个节点忽然挂了,运维部署上依赖的某一个东西不存在了等等场景.本文主要来讨论一下 ...
- csharp:ASP.NET SignalR
http://signalr.net/ https://github.com/SignalR/SignalR http://www.asp.net/signalr http://www.cnblogs ...