码迷,mamicode.com
首页 > 编程语言 > 详细

RunLoop与线程保活

时间:2016-11-13 19:05:08      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:void   ace   代码   selector   load   atom   uitouch   run   view   

 代码:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) NSThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(start) object:nil];
    thread.name = @"My Thread";
    [thread start];
    
    self.thread = thread;
}

- (void)start {
    NSLog(@"%s - %@", __FUNCTION__, [NSThread currentThread].name);
    
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    // Adds a port as an input source to the specified mode of the run loop
    [currentRunLoop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    [currentRunLoop run];
}

- (void)run {
    NSLog(@"%s - %@", __FUNCTION__, [NSThread currentThread].name);
}

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

@end

输出:

-[ViewController start] - My Thread
-[ViewController run] - My Thread
-[ViewController run] - My Thread
-[ViewController run] - My Thread

 

RunLoop与线程保活

标签:void   ace   代码   selector   load   atom   uitouch   run   view   

原文地址:http://www.cnblogs.com/loftyspirit/p/6059334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!