首页 技术 正文
技术 2022年11月16日
0 收藏 995 点赞 2,355 浏览 3954 个字

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

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

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

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

//
// 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

上面的代码知识简单的实现了线程的使用。

下面是其效果图(注意线程的销毁)

IOS RunLoop 常驻线程的实现

实际上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

IOS RunLoop 常驻线程的实现

由效果图我们可以发现。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

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]; _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

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]; _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 常驻线程的实现

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727