美文网首页
GCD(多线程)

GCD(多线程)

作者: 普阳 | 来源:发表于2016-07-20 14:39 被阅读29次

    系统提供的dispatch方法

    //后台执行:

    dispatch_async(dispatch_get_global_queue(0,0), ^{

    //something

    });

    //主线程执行

    dispatch_async(dispatch_get_main_queue(),^{

    //something

    });

    //一次性执行  

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    //code to be executed once

    });

    //延迟两秒执行

    double delayInSecond = 2.0f;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSecond * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^{

    //code to executed on the main queue after delay

    });

    //如果要自定义queue, 可以用dispatch_queue_create方法

    dispatch_queue_t myQueue = dispatch_queue_create("www.baidu.com", NULL);

    dispatch_async(myQueue, ^{

    //your code

    });

    相关文章

      网友评论

          本文标题:GCD(多线程)

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