- (IBAction)createBtnClick:(id)sender {
//1.创建线程
self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(task1) object:nil];
[self.thread start];
}
- (IBAction)otherBtnClick:(id)sender {
[self performSelector:@selector(task2) onThread:self.thread withObject:nil waitUntilDone:YES];
}
-(void)task1
{
//1.获得子线程对应的runloop
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
//保证runloop不退出
//NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
//[runloop addTimer:timer forMode:NSDefaultRunLoopMode];
[runloop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//2.默认是没有开启 仅限子线程 主线程是没用的
[runloop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
-(void)task2
{
NSLog(@"task2---%@",[NSThread currentThread]);
}
-(void)run
{
NSLog(@"%s",__func__);
}