线程常驻,正如其名,我们要实现的事让一个线程长期存在,不被销毁。
这时会有人说,那还不简单吗。
但是这里我们要实现的事如何让线程座椅待命,而且并不是主线程。
首先介绍一下正常情况下的线程使用。
// // 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 (1) { //添加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
时间: 2024-10-07 05:31:33