国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

詳解IOS串行隊列與并行隊列進行同步或者異步的實例

2020-07-26 02:44:29
字體:
來源:轉載
供稿:網友

詳解IOS串行隊列與并行隊列進行同步或者異步的實例

IOS中GCD的隊列分為串行隊列和并行隊列,任務分為同步任務和異步任務,他們的排列組合有四種情況,下面分析這四種情況的工作方式。

同步任務,使用GCD dispatch_sync 進行派發任務

- (void)testSync {  dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL);  dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT);  NSLog(@"====serialQueue====");  for (int i = 0; i<10; i++) {    dispatch_sync(serialQueue, ^{      [NSThread sleepForTimeInterval:0.3];      NSLog(@"==>%@ sync serial XXX>%d", [NSThread currentThread], i);    });  }  NSLog(@"====concurrentQueue====");  for (int i = 0; i<10; i++) {    dispatch_sync(concurrentQueue, ^{      [NSThread sleepForTimeInterval:0.3];      NSLog(@"==>%@ sync concurrent ====>%d", [NSThread currentThread], i*i);    });  }}

結果如下:

2017-03-01 01:36:22.835 Demo ====serialQueue====2017-03-01 01:36:23.207 {number = 1, name = main} sync serial XXX>02017-03-01 01:36:23.578 {number = 1, name = main} sync serial XXX>12017-03-01 01:36:23.952 {number = 1, name = main} sync serial XXX>22017-03-01 01:36:24.325 {number = 1, name = main} sync serial XXX>32017-03-01 01:36:24.699 {number = 1, name = main} sync serial XXX>42017-03-01 01:36:25.072 {number = 1, name = main} sync serial XXX>52017-03-01 01:36:25.446 {number = 1, name = main} sync serial XXX>62017-03-01 01:36:25.746 {number = 1, name = main} sync serial XXX>72017-03-01 01:36:26.122 {number = 1, name = main} sync serial XXX>82017-03-01 01:36:26.489 {number = 1, name = main} sync serial XXX>92017-03-01 01:36:26.489 Demo ====concurrentQueue====2017-03-01 01:36:26.864 {number = 1, name = main} sync concurrent ====>02017-03-01 01:36:27.236 {number = 1, name = main} sync concurrent ====>12017-03-01 01:36:27.611 {number = 1, name = main} sync concurrent ====>42017-03-01 01:36:27.985 {number = 1, name = main} sync concurrent ====>92017-03-01 01:36:28.354 {number = 1, name = main} sync concurrent ====>162017-03-01 01:36:28.726 {number = 1, name = main} sync concurrent ====>252017-03-01 01:36:29.100 {number = 1, name = main} sync concurrent ====>362017-03-01 01:36:29.474 {number = 1, name = main} sync concurrent ====>492017-03-01 01:36:29.849 {number = 1, name = main} sync concurrent ====>642017-03-01 01:36:30.223 {number = 1, name = main} sync concurrent ====>81

testSync方法是在主線程中調用的,結果看到使用的串行隊列和使用并行隊列看到的結果都是發生在當前線程:主線程中,沒有開啟新的線程處理任務,任務的調度也是串行調度的。

異步任務,使用GCD dispatch_async 進行派發任務

- (void)testAsync {  dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL);  dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT);  NSLog(@"====serialQueue====");  for (int i = 0; i<10; i++) {    dispatch_async(serialQueue, ^{      [NSThread sleepForTimeInterval:0.3];      NSLog(@"==>%@ async serial XXX>%d", [NSThread currentThread], i);    });  }  NSLog(@"====concurrentQueue====");  for (int i = 0; i<10; i++) {    dispatch_async(concurrentQueue, ^{      [NSThread sleepForTimeInterval:0.3];      NSLog(@"==>%@ async concurrent ====>%d", [NSThread currentThread], i*i);    });  }}`

結果如下:

2017-03-01 01:45:36.125 Demo ====serialQueue====2017-03-01 01:45:36.125 Demo ====concurrentQueue====2017-03-01 01:45:36.494 {number = 3, name = (null)} async concurrent ====>02017-03-01 01:45:36.494 {number = 5, name = (null)} async concurrent ====>42017-03-01 01:45:36.494 {number = 4, name = (null)} async concurrent ====>12017-03-01 01:45:36.494 {number = 6, name = (null)} async concurrent ====>162017-03-01 01:45:36.494 {number = 8, name = (null)} async serial XXX>02017-03-01 01:45:36.494 {number = 7, name = (null)} async concurrent ====>92017-03-01 01:45:36.494 {number = 9, name = (null)} async concurrent ====>252017-03-01 01:45:36.494 {number = 11, name = (null)} async concurrent ====>492017-03-01 01:45:36.494 {number = 10, name = (null)} async concurrent ====>362017-03-01 01:45:36.501 {number = 13, name = (null)} async concurrent ====>812017-03-01 01:45:36.501 {number = 12, name = (null)} async concurrent ====>642017-03-01 01:45:36.869 {number = 8, name = (null)} async serial XXX>12017-03-01 01:45:37.244 {number = 8, name = (null)} async serial XXX>22017-03-01 01:45:37.615 {number = 8, name = (null)} async serial XXX>32017-03-01 01:45:37.986 {number = 8, name = (null)} async serial XXX>42017-03-01 01:45:38.358 {number = 8, name = (null)} async serial XXX>52017-03-01 01:45:38.730 {number = 8, name = (null)} async serial XXX>62017-03-01 01:45:39.103 {number = 8, name = (null)} async serial XXX>72017-03-01 01:45:39.472 {number = 8, name = (null)} async serial XXX>82017-03-01 01:45:39.842 {number = 8, name = (null)} async serial XXX>9

testSync方法是在主線程中調用的,結果看到使用的串行隊列的異步任務會開啟一個子線程執行任務,任務的調度是串行的
使用并行隊列的異步任務會開啟多個子線程并行的處理任務,任務的先后順序是不固定的,任務的調度方式是并行的

總結

同步任務:和使用的隊列無關,不會開啟子線程處理任務,會在當前的線程中串行的調度任務,即一個任務完成之后繼續下一個任務,如果同步任務在主線程中調用,會阻塞主線程

異步任務:a. 使用串行隊列,會開啟一個子線程串行的調度任務 b. 使用并行隊列,會開啟多個子線程并行的調度任務,這種情況用的是最多的。

以上就是對詳解IOS串行隊列與并行隊列進行同步或者異步的實例,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 迁西县| 祁连县| 汤阴县| 徐州市| 托克托县| 兰溪市| 广昌县| 介休市| 卫辉市| 咸宁市| 乐清市| 宜宾县| 兴海县| 东安县| 青河县| 通渭县| 招远市| 白水县| 织金县| 双辽市| 伊川县| 双辽市| 赞皇县| 邵东县| 墨江| 育儿| 逊克县| 安乡县| 临猗县| 如皋市| 桑植县| 盖州市| 崇文区| 仙桃市| 社会| 花莲市| 永济市| 双鸭山市| 金寨县| 蕲春县| 安图县|