多线程

OC中创建线程的方法
如果指定在主线程中执行代码,如何延时执行代码

  • 1.创建线程的方法

    • NSThread
    • NSOperationQueue、NSOperation
    • GCD
  • 2.在主线程中执行代码

[self performSelector: onThread:[NSThread mainThread] withObject: waitUntilDone:];
dispatch_async(dispatch_get_main_queue(), ^{});
  • 3.延时执行
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        
});

[self performSelector: withObject: afterDelay:];
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:];

你可能感兴趣的:(多线程)