zoukankan      html  css  js  c++  java
  • RunLoop与线程保活

     代码:

    #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
  • 相关阅读:
    convex hull
    不努力,你就没有资格谈天赋
    “数数”的感悟
    平行四边形法则的感悟
    ansys 15 Linux 安装问题
    SHV-E210S 刷4.3
    fedora 安装 Oracle11g 遇到的问题
    ObjectArx开发常用资料
    MFC入门(二)
    MFC入门(三)
  • 原文地址:https://www.cnblogs.com/xwoder/p/6059334.html
Copyright © 2011-2022 走看看