线程常驻,正如其名,我们要实现的事让一个线程长期存在,不被销毁。

这时会有人说,那还不简单吗。

但是这里我们要实现的事如何让线程座椅待命,而且并不是主线程。

首先介绍一下正常情况下的线程使用。

//
// 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 常驻线程的实现的更多相关文章

  1. iOS之创建一个常驻线程

    // 当创建一个线程,并且希望它一直存在时,但往往我们创建的线程都是执行完成之后也就停止了,不能再次利用,那么如何创建一个线程可以让他可以再次工作呢,这个时候就需要使用到RunLoop了.下面的是我写 ...

  2. iOS Runloop理解

    一.RunLoop的定义 当有持续的异步任务需求时,我们会创建一个独立的生命周期可控的线程.RunLoop就是控制线程生命周期并接收事件进行处理的机制. RunLoop是iOS事件响应与任务处理最核心 ...

  3. IOS多线程之线程的创建

    版权声明:原创作品,谢绝转载!否则将追究法律责任.   之前也说过线程是消耗资源的.多线程会占用你应用程序(和系统的)的内存使用和性能方面的资源.我们创建一个线程后可以对他的一些部分进行配置例如可以对 ...

  4. iOS RunLoop详解

    1. RunLoop简介 1.1 什么是RUnLoop 可以理解为字面的意思:Run表示运行,Loop表示循环.结合在一起就是运行的循环.通常叫做运行循环. RunLoop实际上是一个对象,这个对象在 ...

  5. iOS Runloop 消息循环

    介绍 Runloop是一种事件监听循环,可以理解成一个while死循环,监听到事件就起来,没有就休息. Runloop可以在不同模式下进行切换,iOS有五种模式,其中UIInitializationR ...

  6. iOS runLoop 原理多线程 总结 NSTimer优化

    可以理解为字面意思:Run 表示运行,Loop 表示循环.结合在一起就是运行的循环的意思.哈哈,我更愿意翻译为『跑圈』.直观理解就像是不停的跑圈. RunLoop 实际上是一个对象,这个对象在循环中用 ...

  7. IOS RunLoop面试题

    一 什么是RunLoop? 从字面意思看就是运行循环,其实内部就是do-while循环,这个循环内部不断地处理各种任务(比 如Source,Timer,Observer) 一个线程对应一个RunLoo ...

  8. ios -RunLoop(简单理解)

    一. RunLoop简介 RunLoop字面意思是运行时,即跑圈得意思.它可以在我们需要的时候自己跑起来运行,在我们没有操作的时候就停下来休息,充分节省CPU资源,提高程序性能. 二. RunLoop ...

  9. ios runloop学习

    今天突然才之间才意识到NSTimer这样的运行方式,是在多线程中实现的循环还是在主线程中去实现的呢.当然不可能是在主线程中的while那么简单,那样什么都干不了,简单看了下NSTimer是以同步方式运 ...

随机推荐

  1. Detach Volume 操作 - 每天5分钟玩转 OpenStack(55)

    上一节我们成功地通过 attach 操作为 instance 添加了 volume,而与之相对的操作是 detach,就是将 volume 从 instance 上卸载下来. 下图是 Detach 操 ...

  2. 关于SubSonic3.0生成的表名自动加复数(s)的“用户代码未处理SqlException,对象名'xxxs'无效”异常处理

    使用SubSonic3.0模版生成时,同2.2版本一样,都会自动在一些类似数据库要用到的关键后面加要s(复数),这里也是3.0的一个小Bug,在查询时由于插件并没有完全的去掉s,所以会产生“用户代码未 ...

  3. ASP.NET:注销功能实现

    原理:清空Session 1.Web窗体:index.aspx <a href="logoutHandler.ashx">注销</a> 2.一般处理程序:L ...

  4. JavaSE高级之集合类

    ​下面的内容是对java中的集合类进行的总结,过段时间会分享java的网路编程,多线程等内容,欢迎批评指正. 1.Java中的集合是用来存放对象的,即集合是对象的集合,对象是集合的元素,java AP ...

  5. iOS 网易新闻用到的框架

    网易新闻iOS版在开发过程中曾经使用过的第三方开源类库.组件 1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务 ...

  6. DotNet中几种常用的加密算法

    在.NET项目中,我们较多的使用到加密这个操作.因为在现代的项目中,对信息安全的要求越来越高,那么多信息的加密就变得至关重要.现在提供几种常用的加密/解密算法. 1.用于文本和Base64编码文本的互 ...

  7. IO多路复用之poll总结

    1.基本知识 poll的机制与select类似,与select在本质上没有多大差别,管理多个描述符也是进行轮询,根据描述符的状态进行处理,但是poll没有最大文件描述符数量的限制.poll和selec ...

  8. CSS3 media queries + jQuery实现响应式导航

    目的: 实现一个响应式导航,当屏幕宽度大于700px时,效果如下: 当屏幕宽度小于700px时,导航变成一个小按钮,点击之后有一个菜单慢慢拉下来: 思路: 1.为了之后在菜单上绑定事件,并且不向DOM ...

  9. JS正则表达式总结

    关于JS的正则用法,已经有很多文章了,大同小异 正则表达式30分钟入门教程 MDN正则表达式 玩转JavaScript正则表达式 ES6正则的扩展

  10. jquery如何判断checkbox(复选框)是否被选中 ...

    <script type="text/javascript" src="../lib/jQuery1.11.1.js"></script> ...