GCD的基本语法

//异步主线程运行,不会产生生死锁

dispatch_sync(dispatch_get_main_queue(), ^{

});

//同步主线程运行,会产生生死锁

dispatch_async(dispatch_get_main_queue(), ^{

});

//从子线程异步返回,网络活动监视器

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

NSURLRequest *webRequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];

[_webView loadRequest:webRequest];

//        dispatch_async(dispatch_get_main_queue(), ^{

//        });

});

//保证线程只运行一次,用于单例模式

dispatch_once_t oneToken;

dispatch_once(&oneToken, ^{

//alloc init

});

//按照顺序执代码--之前代码执行完毕之后在执行这个代码

dispatch_barrier_async(dispatch_queue_create("com.dullgrass.conCurrentQueue", DISPATCH_QUEUE_CONCURRENT), ^{

});

//延迟执行10秒执行Block

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

});

你可能感兴趣的:(GCD的基本语法)