您的位置:首页 > 运维架构

使用runloop阻塞线程的正确写法

2013-02-26 01:09 411 查看

使用runloop阻塞线程的正确写法

http://marshal.easymorse.com/archives/4700
runloop可以阻塞线程,等待其他线程执行后再执行。比如:@implementation ViewController{
BOOL end;
}

– (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@”start new thread …”);
[NSThread detachNewThreadSelector:@selector(runOnNewThread) toTarget:self withObject:nil];
while (!end) {
NSLog(@”runloop…”);
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
NSLog(@”runloop end.”);
}
NSLog(@”ok.”);
}
-(void)runOnNewThread{
NSLog(@”run for new thread …”);
sleep(1);
end=YES;
NSLog(@”end.”);
}但是这样做,运行时会发现,while循环后执行的语句会在很长时间后才被执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: