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

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

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

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

//
// 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. 优化javaScript代码,提高执行效率

    今天看完书,总结了一下可以如何优化 JavaScript . 1.合并js文件 为优化性能,可以把多个js文件(css文件也可以)合并成极少数大文件.跟十个5k的js文件相比,合并成一个50k的文件更 ...

  2. T-Sql(六)触发器(trigger)

    不知不觉讲到触发器了,一般我们做程序的很少接触到触发器,触发器的操作一般是DB人员来完成. 然而有的时候一些简单的业务需要我们自己去完成,不能每次都去麻烦DB人员,所以说,编程人员要全才,除了编程以为 ...

  3. iOS chart 图表完美解决方案 基于swift

    如果打算在app中使用图标功能,这个框架基本能够满足90%的需求 下边是作者的框架的下载地址 ,基于swift2.0 https://github.com/danielgindi/ios-charts ...

  4. 一台电脑上的git同时使用两个github账户

    需求: 公司有github账号,自己有github账号,想在git上同时使用,两者互不干扰. 思路: 管理两个SHH key. 解决方案: 一.生成两个SSH key 为了举例方便,这里使用“one” ...

  5. (十)WebGIS中地理坐标与屏幕坐标间的转换原理

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 地图本身是拥有坐标的,一般可以大致分为平面坐标和经纬度坐标, ...

  6. ASP.NET Core中的ActionFilter与DI

    一.简介 前几篇文章都是讲ASP.NET Core MVC中的依赖注入(DI)与扩展点的,也许大家都发现在ASP.NET CORE中所有的组件都是通过依赖注入来扩展的,而且面向一组功能就会有一组接口或 ...

  7. Sublime插件支持Sass编译和Babel解析ES6 & .sublime-build文件初探

    用Sublime Text蛮久了,配置配来配去的,每次换电脑都得重头再配过,奈何人老了脑子不中用了,得好好整理一些,下次换电脑就有得参考了.. 同事说,他的WebStorm简直太方便,自身集成了很多方 ...

  8. 数字限时增长效果实现:numberGrow.js

    这是上周工作中写到的一个功能,大概的效果就是页面中有几处数字,统计公司的一些业务信息,需要在第一次出现的时候,做一个从0开始增长,大概2秒自动增长到真实数值,并停止增长的效果.这个问题的重点在于解决如 ...

  9. Objective-C 对象(内容根据iOS编程编写)

    开发iOS程序需要使用 Objective-C 语言和Cocoa Touch框架.Objective-C 源于 C 语言,是 C 语言的扩展. Cocoa Touch框架是一个Objective-C类 ...

  10. 6.Configure Domain Classes(配置领域类)【EF Code-First 系列】

    在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型. Code-First模式,发起了一种编程模式:约定大于配置.这也就是说, ...