1、
Thread启动后,线程的工作入口代码有3个地方可以选择。如果实现了main,则block不会被调用。
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument
- (instancetype)initWithBlock:(void (^)(void))block
//thread body method
- (void)main
2、
保活线程的两种做法,用while,或者开启runloop。
否则,即使线程即使保持强引用,其实际线程的资源已被释放了。在perfer系列时会因一个已经释放线程上下文环境的thread而crash.
这两种的区别在于,runloop可以支持NSThreadPerformAdditions,NSTimer。 while循环的则不支持
int i = 0;
while (1) {
DDLogInfo(@"%d", i++);
sleep(1);
}
NSRunLoop*runloop=[NSRunLoop currentRunLoop];
NSPort*port=[NSPort port];
[runloop addPort:port forMode:NSRunLoopCommonModes];
[runloop run];
NSTimer*timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerevent:) userInfo:nil repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
[runLoop run];
3、