GCD(多线程)
2016-07-20 本文已影响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
});