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 (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
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值