日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GCD牛逼的中枢调度器

發(fā)布時(shí)間:2023/11/29 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GCD牛逼的中枢调度器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

GCD的基本使用:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {// dispatch_sync : 同步,不具備開啟線程的能力// dispatch_async : 異步,具備開啟線程的能力// 并發(fā)隊(duì)列 :多個(gè)任務(wù)可以同時(shí)執(zhí)行// 串行隊(duì)列 :一個(gè)任務(wù)執(zhí)行完后,再執(zhí)行下一個(gè)任務(wù)// 獲得全局的并發(fā)隊(duì)列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務(wù) 添加 全局隊(duì)列 中去 異步 執(zhí)行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中隊(duì)列的使用:

// dispatch_sync : 同步,不具備開啟線程的能力 // dispatch_async : 異步,具備開啟線程的能力// 并發(fā)隊(duì)列 :多個(gè)任務(wù)可以同時(shí)執(zhí)行 // 串行隊(duì)列 :一個(gè)任務(wù)執(zhí)行完后,再執(zhí)行下一個(gè)任務(wù)// Foundation : OC // Core Foundation : C語言 // Foundation和Core Foundation框架的數(shù)據(jù)類型可以互相轉(zhuǎn)換的//NSString *str = @"123"; // Foundation //CFStringRef str2 = (__bridge CFStringRef)str; // Core Foundation //NSString *str3 = (__bridge NSString *)str2; // CFArrayRef ---- NSArray // CFDictionaryRef ---- NSDictionary // CFNumberRef ---- NSNumber// Core Foundation中手動創(chuàng)建的數(shù)據(jù)類型,都需要手動釋放 // CFArrayRef array = CFArrayCreate(NULL, NULL, 10, NULL); // CFRelease(array); // // // CGPathRef path = CGPathCreateMutable(); // CGPathRetain(path); // // CGPathRelease(path); // CGPathRelease(path); /**凡是函數(shù)名中帶有create\copy\new\retain等字眼, 都應(yīng)該在不需要使用這個(gè)數(shù)據(jù)的時(shí)候進(jìn)行releaseGCD的數(shù)據(jù)類型在ARC環(huán)境下不需要再做releaseCF(Core Foundation)的數(shù)據(jù)類型在ARC\MRC環(huán)境下都需要再做release*/#import "HMViewController.h"@interface HMViewController ()@end@implementation HMViewController- (void)viewDidLoad {[super viewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[self asyncSerialQueue]; }/*** async -- 并發(fā)隊(duì)列(最常用)* 會不會創(chuàng)建線程:會,一般同時(shí)開多條* 任務(wù)的執(zhí)行方式:并發(fā)執(zhí)行*/ - (void)asyncGlobalQueue {// 獲得全局的并發(fā)隊(duì)列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務(wù) 添加 全局隊(duì)列 中去 異步 執(zhí)行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 -- 串行隊(duì)列(有時(shí)候用)* 會不會創(chuàng)建線程:會,一般只開1條線程* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))*/ - (void)asyncSerialQueue {// 1.創(chuàng)建一個(gè)串行隊(duì)列dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);// 2.將任務(wù)添加到串行隊(duì)列中 異步 執(zhí)行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,需要釋放創(chuàng)建的隊(duì)列 // dispatch_release(queue); }/*** async -- 主隊(duì)列(很常用)*/ - (void)asyncMainQueue {// 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù),都會自動放到主線程中去執(zhí)行)dispatch_queue_t queue = dispatch_get_main_queue();// 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行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 -- 主隊(duì)列(不能用---會卡死)*/ - (void)syncMainQueue {NSLog(@"syncMainQueue----begin--");// 1.主隊(duì)列(添加到主隊(duì)列中的任務(wù),都會自動放到主線程中去執(zhí)行)dispatch_queue_t queue = dispatch_get_main_queue();// 2.添加 任務(wù) 到主隊(duì)列中 異步 執(zhí)行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 -- 并發(fā)隊(duì)列* 會不會創(chuàng)建線程:不會* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))* 并發(fā)隊(duì)列失去了并發(fā)的功能*/ - (void)syncGlobalQueue {// 獲得全局的并發(fā)隊(duì)列dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 將 任務(wù) 添加到 全局并發(fā)隊(duì)列 中 同步 執(zhí)行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 -- 串行隊(duì)列* 會不會創(chuàng)建線程:不會* 任務(wù)的執(zhí)行方式:串行執(zhí)行(一個(gè)任務(wù)執(zhí)行完畢后再執(zhí)行下一個(gè)任務(wù))*/ - (void)syncSerialQueue {// 創(chuàng)建一個(gè)串行隊(duì)列dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);// 將 任務(wù) 添加到 串行隊(duì)列 中 同步 執(zhí)行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

?

轉(zhuǎn)載于:https://www.cnblogs.com/ZMiOS/p/4924219.html

總結(jié)

以上是生活随笔為你收集整理的GCD牛逼的中枢调度器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。