只要线程中有一个启动重的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]);
}
网友评论