GCD牛逼的中枢调度器
生活随笔
收集整理的這篇文章主要介紹了
GCD牛逼的中枢调度器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GCD的基本使用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {// dispatch_sync : 同步,不具備開啟線程的能力// dispatch_async : 異步,具備開啟線程的能力// 并發隊列 :多個任務可以同時執行// 串行隊列 :一個任務執行完后,再執行下一個任務// 獲得全局的并發隊列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務 添加 全局隊列 中去 異步 執行dispatch_async(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);}); }GCD中隊列的使用:
// dispatch_sync : 同步,不具備開啟線程的能力 // dispatch_async : 異步,具備開啟線程的能力// 并發隊列 :多個任務可以同時執行 // 串行隊列 :一個任務執行完后,再執行下一個任務// Foundation : OC // Core Foundation : C語言 // Foundation和Core Foundation框架的數據類型可以互相轉換的//NSString *str = @"123"; // Foundation //CFStringRef str2 = (__bridge CFStringRef)str; // Core Foundation //NSString *str3 = (__bridge NSString *)str2; // CFArrayRef ---- NSArray // CFDictionaryRef ---- NSDictionary // CFNumberRef ---- NSNumber// Core Foundation中手動創建的數據類型,都需要手動釋放 // CFArrayRef array = CFArrayCreate(NULL, NULL, 10, NULL); // CFRelease(array); // // // CGPathRef path = CGPathCreateMutable(); // CGPathRetain(path); // // CGPathRelease(path); // CGPathRelease(path); /**凡是函數名中帶有create\copy\new\retain等字眼, 都應該在不需要使用這個數據的時候進行releaseGCD的數據類型在ARC環境下不需要再做releaseCF(Core Foundation)的數據類型在ARC\MRC環境下都需要再做release*/#import "HMViewController.h"@interface HMViewController ()@end@implementation HMViewController- (void)viewDidLoad {[super viewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[self asyncSerialQueue]; }/*** async -- 并發隊列(最常用)* 會不會創建線程:會,一般同時開多條* 任務的執行方式:并發執行*/ - (void)asyncGlobalQueue {// 獲得全局的并發隊列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務 添加 全局隊列 中去 異步 執行dispatch_async(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);}); }/*** async -- 串行隊列(有時候用)* 會不會創建線程:會,一般只開1條線程* 任務的執行方式:串行執行(一個任務執行完畢后再執行下一個任務)*/ - (void)asyncSerialQueue {// 1.創建一個串行隊列dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);// 2.將任務添加到串行隊列中 異步 執行dispatch_async(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);});// 3.非ARC,需要釋放創建的隊列 // dispatch_release(queue); }/*** async -- 主隊列(很常用)*/ - (void)asyncMainQueue {// 1.主隊列(添加到主隊列中的任務,都會自動放到主線程中去執行)dispatch_queue_t queue = dispatch_get_main_queue();// 2.添加 任務 到主隊列中 異步 執行dispatch_async(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_async(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);}); }/*** sync -- 主隊列(不能用---會卡死)*/ - (void)syncMainQueue {NSLog(@"syncMainQueue----begin--");// 1.主隊列(添加到主隊列中的任務,都會自動放到主線程中去執行)dispatch_queue_t queue = dispatch_get_main_queue();// 2.添加 任務 到主隊列中 異步 執行dispatch_sync(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});NSLog(@"syncMainQueue----end--"); }/**-------------------------------------華麗的分割線-----------------------------------------------------**//*** sync -- 并發隊列* 會不會創建線程:不會* 任務的執行方式:串行執行(一個任務執行完畢后再執行下一個任務)* 并發隊列失去了并發的功能*/ - (void)syncGlobalQueue {// 獲得全局的并發隊列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務 添加到 全局并發隊列 中 同步 執行dispatch_sync(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);}); }/*** sync -- 串行隊列* 會不會創建線程:不會* 任務的執行方式:串行執行(一個任務執行完畢后再執行下一個任務)*/ - (void)syncSerialQueue {// 創建一個串行隊列dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);// 將 任務 添加到 串行隊列 中 同步 執行dispatch_sync(queue, ^{NSLog(@"-----下載圖片1---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片2---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片3---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片4---%@", [NSThread currentThread]);});dispatch_sync(queue, ^{NSLog(@"-----下載圖片5---%@", [NSThread currentThread]);}); }@end?
轉載于:https://www.cnblogs.com/ZMiOS/p/4924219.html
總結
以上是生活随笔為你收集整理的GCD牛逼的中枢调度器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到签单成功好不好
- 下一篇: 梦到两条大蟒蛇是什么意思