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是以同步方式运 ...
随机推荐
- 用Canvas+Javascript FileAPI 实现一个跨平台的图片剪切、滤镜处理、上传下载工具
直接上代码,其中上传功能需要自己配置允许跨域的文件服务器地址~ 或者将html文件贴到您的站点下同源上传也OK. 支持: 不同尺寸图片获取. 原图缩小放大. 原图移动. 选择框大小改变. 下载选中的区 ...
- 【经验之谈】Git使用之Windows环境下配置
前言 安装 配置 关于git使用的几个问题 后记 关于代码托管,以前用过vss和svn,看博客或论坛的时候,经常有人提到github,有很多著名的开源软件都托管在github,想来肯定不错(莫笑),当 ...
- Windows Server 2012 为什么没有“磁盘清理”选项了?
用习惯了客户端版的Windows,对于磁盘清理想必大家都不会陌生,他具有安全.快捷.准确.集中化的删除系统中的临时文件.管理系统还原卷影副本.添加删除程序的快捷入口等便捷功能,而在Server版的Wi ...
- 1Z0-053 争议题目解析330
1Z0-053 争议题目解析330 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 330.What will be the end result of this set of RM ...
- 相克军_Oracle体系_随堂笔记012-undo
undo表空间中undo段是自动生成的,oracle自动使用undo表空间的undo段. 作为高级DBA,需要了解Oracle是如何使用undo段的.这样出了性能问题才能够解决. 1.Undo表空 ...
- hibernate笔记--缓存机制之 一级缓存(session缓存)
一级缓存: 又称为session缓存,它和session生命周期相同,周期非常短.是事务级别的缓存: 还是以Book和Category这两个表为例,我们用代码观察一个缓存的存在: 假设现在我要去查询i ...
- css常用的特效代码
一.网页变灰的代码:a) 网页变灰色<head>加到这里</head><style type="text/css">html {FILTER: ...
- C语言字符串匹配、goto语句、关机命令使用
1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...
- ThinkPHP学习(二)
开发规范 1.命名规范 使用ThinkPHP开发的过程中应该尽量遵循下列命名规范: 特例:在ThinkPHP里面,有一个函数命名的特例,就是单字母大写函数,这类函数通常是某些操作的快 捷定义,或者有特 ...
- 你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)
一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什 ...