常驻线程

2017-04-10  本文已影响0人  AlerStar

#import "ViewController.h"

@interface ViewController ()

/**
 *  常驻线程
 */
@property(strong,nonatomic) NSThread * myThread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatMyThread];
    
}

#pragma mark - System Method

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(runTwo) onThread:self.myThread withObject:nil waitUntilDone:NO];
}

- (void)runTwo
{
    NSLog(@"Run_Two");
    NSLog(@"常驻线程任务执行中");
}


#pragma mark - Private Method

/**
 *  创建并且开启常驻线程
 */
- (void)creatMyThread
{
    self.myThread = [[NSThread alloc]initWithTarget:self selector:@selector(runOne) object:nil];
    
    [self.myThread start];
}

- (void)runOne
{
    // 获取RunLoop (获取时创建RunLoop)
    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
    
    NSLog(@"RunLoop");
}

@end
上一篇下一篇

猜你喜欢

热点阅读