[self performSelectorInBackground:@selector(multiThread) withObject:nil];
- (void)multiThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//此种方式创建的timer已经添加至runLoop
// [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//此种方式创建的timer没有添加至runLoop
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//将定时器添加到RunLoop中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[pool release];
[[NSRunLoop currentRunLoop] run];
NSLog(@"线程结束");
}
- (void)timerAction {
NSLog(@"timerAction");
}
本文介绍了一个使用Objective-C实现的多线程方法,并在该线程中通过NSTimer设置了一个重复触发的定时器任务。演示了如何将定时器加入到RunLoop中并运行。
1540

被折叠的 条评论
为什么被折叠?



