runloop 阻塞主线程

  1. @implementation ViewController{  
  2.     BOOL end;  
  3. }  
  4. – (void)viewDidLoad  
  5. {  
  6.     [super viewDidLoad];   
  7.     NSLog(@”start new thread …”);  
  8.     [NSThread detachNewThreadSelector:@selector(runOnNewThread) toTarget:self withObject:nil];      
  9.     while (!end) {  
  10.         NSLog(@”runloop…”);  
  11.         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  12.         NSLog(@”runloop end.”);  
  13.     }  
  14.     NSLog(@”ok.”);  
  15. }  
  16.   
  17. -(void)runOnNewThread{  
  18.     NSLog(@”run for new thread …”);  
  19.     sleep(1);  
  20.     [self performSelectorOnMainThread:@selector(setEnd) withObject:nil waitUntilDone:NO];  
  21.     NSLog(@”end.”);  
  22. }  
  23. -(void)setEnd{  
  24.     end=YES;  

你可能感兴趣的:(iPhone,开发)