美文网首页
NSThread&RunLoop&AutoreleasePool

NSThread&RunLoop&AutoreleasePool

作者: Code_人生 | 来源:发表于2019-10-08 16:41 被阅读0次
  • 1、每一个线程都会维护自己的自动释放池
  • 2、每一个 autoreleasePool 对应一个线程
  • 3、RunLoop与自动释放池的关系和线程与自动释放池类似,因为RunLoop和线程是一一对应的

一、一般线程

    [NSThread detachNewThreadSelector:@selector(test_one) toTarget:self withObject:nil];

- (void)test_one{
    //一般线程
    __autoreleasing id test = [NSObject new];
    NSLog(@"obj=%@",test);
    tmp123 = test;
    [[NSThread currentThread] setName:@"test runloop thread"];
    NSLog(@"thread ending");
}

二、常驻线程

    [NSThread detachNewThreadSelector:@selector(test_one) toTarget:self withObject:nil];

- (void)test_one{    
    //常驻线程
    //timerc常驻了!
    [[NSThread currentThread] setName:@"test runloop thread"];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];
}

- (void)testAction{
    __autoreleasing id test = [NSObject new];
    NSLog(@"obj=%@",test);
    tmp123 = test;
}

1、子线程在使用 autorelease 对象,懒加载出来一个 autoreleasePoolPage
2、pop 操作
2.1线程exit时,释放当前的资源(tls)
2.2RunLoop退出的时候,Pop 操作

三、应用场景

  • 1、循环当中
  • 2、创建新的线程
  • 3、长时间在后台运行的任务~

相关文章

网友评论

      本文标题:NSThread&RunLoop&AutoreleasePool

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