多线程

作者: March_Cullen | 来源:发表于2017-03-06 09:48 被阅读0次

    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:];
    

    相关文章

      网友评论

          本文标题:多线程

          本文链接:https://www.haomeiwen.com/subject/zdpygttx.html