常驻线程


#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 *)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

你可能感兴趣的:(常驻线程)