美文网首页
基于RunLoop 实现 线程保活

基于RunLoop 实现 线程保活

作者: tp夕阳武士 | 来源:发表于2019-08-14 20:01 被阅读0次

    只要线程中有一个启动重的runloop,则线程可以持续保活;
    一个能持续运行的runloop想要有至少一个mode;
    一个mode中至少有一个input source 或者一个 timer source;

    实现代码:

    -(void)viewDidLoad{
        [super viewDidLoad];
        [self task1];
    }
    
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self performSelector:@selector(task2) onThread:self.activeT withObject:nil waitUntilDone:YES];
    }
    
    -(void)task1{
        self.activeT = [[NSThread alloc] initWithBlock:^{
            NSLog(@"start%@",[NSThread currentThread]);
            [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSRunLoopCommonModes];
            [[NSRunLoop currentRunLoop] run];
            NSLog(@"end");
        }];
        [self.activeT start];
    }
    
    -(void)task2{
        NSLog(@"task2%@",[NSThread currentThread]);
    }  
    

    相关文章

      网友评论

          本文标题:基于RunLoop 实现 线程保活

          本文链接:https://www.haomeiwen.com/subject/sjgnjctx.html