IOS中級篇 —— 多線程,ios多線程

來源:互聯網
上載者:User

IOS中級篇 —— 多線程,ios多線程

GCD 是c語言的架構,不需要手動管理記憶體 是一個面向任務   不是面向線程,不需要管理線程的生命週期 GCD 任務/隊列 執行函數任務:Block  任務都封閉在Block中。  —— 線程執行 隊列:存放任務    FIFO (先進先出的原則)        GCD中的隊列:                                  串列隊列:想要任務按順序執行//    建立一個串列隊列    dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);                                  並發隊列:想要任務並發執行(同時執行)//    建立一個並發隊列    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);兩個特殊隊列          主隊列:主隊列中存放的任務最終會交給主線程來執行。//    擷取主隊列  不需要建立    dispatch_queue_t mainQueue = dispatch_get_main_queue();          全域並發隊列:也是並發隊列 沒有任何區別。 平常開發用非常多。 (不需要管理 程式需要開多少條線程。 會按照當前的裝置來自動開線程          //    擷取全域並發隊列  不需要建立//    long identifier  ios8之前 傳線程優先順序 DISPATCH_QUEUE_PRIORITY_DEFAULT   ios8以後傳0 可以相容之前的版本//    <#unsigned long flags#> 保留介面 還未用  傳 0    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0); 執行函數: 將隊列中的任務放到線程中執行                    同步執行函數:不具備開啟新線程的能力。任務中會在當前線程執行。//      同步    dispatch_sync(concurrentQueue, ^{        NSLog(@"下載圖片2");    });同步函數 + 並發隊列 和 + 串列隊列 效果是一樣的                     非同步執行函數:具備開啟新線程能力。 是否開啟線程不一定。 與隊列有關//    執行函數//      非同步執行//      dispatch_queue_t queue 隊列//      ^(void)block    任務    dispatch_async(serialQueue, ^{        NSLog(@"下載圖片");    }); GCD的作用:     1>將任務添加到隊列中     2>用執行函數執行任務中的任務 2個執行函數 * 3個隊列  同步函數例子
 1 // 同步函數 + 串列隊列 2 // 不會開啟新線程,任務是在當前線程執行. 3 // 如果當前線程是子線程的話,就是在子線程執行 4 // 任務是按順序執行的 5 -(void)test1 6 { 7     dispatch_queue_t serialQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL); 8     // 同步函數 9     dispatch_sync(serialQueue, ^{10         // 任務11         NSLog(@"下載圖片1%@",[NSThread currentThread]);12     });13     // 同步函數14     dispatch_sync(serialQueue, ^{15         // 任務16         NSLog(@"下載圖片2%@",[NSThread currentThread]);17     });18     // 同步函數19     dispatch_sync(serialQueue, ^{20         // 任務21         NSLog(@"下載圖片3%@",[NSThread currentThread]);22     });23 }24  25 // 同步函數 + 全域並發隊列26 // 不會開啟新線程,在當前線程執行27 // 如果當前線程是子線程的話,就是在子線程執行28 // 任務按順序執行29 - (void)test230 {31     dispatch_queue_t queue = dispatch_get_global_queue(0, 0);32     dispatch_sync(queue, ^{33         NSLog(@"下載圖片1%@",[NSThread currentThread]);34     });35     dispatch_sync(queue, ^{36         NSLog(@"下載圖片2%@",[NSThread currentThread]);37     });38     dispatch_sync(queue, ^{39         NSLog(@"下載圖片3%@",[NSThread currentThread]);40     });41 }42 // 對於同步函數 + 串列隊列 和 +並發隊列 執行效果是一樣的.43 // 同步函數 +主隊列44 // 主隊列中的任務都會交給主線程來執行.45 // 主線程中的任務和主隊列中的任務相互等待,無法執行完畢46 - (void)test347 {48     dispatch_sync(dispatch_get_main_queue(), ^{49         NSLog(@"下載圖片1%@",[NSThread currentThread]);50     });51     dispatch_sync(dispatch_get_main_queue(), ^{52         NSLog(@"下載圖片2%@",[NSThread currentThread]);53     });54     dispatch_sync(dispatch_get_main_queue(), ^{55         NSLog(@"下載圖片3%@",[NSThread currentThread]);56     });57     NSLog(@"test3End%@",[NSThread currentThread]);58 59 }

 

非同步函數例子   
 1 // 非同步函數 + 主隊列 2     // 不會開啟新線程 3     // 任務按順序執行 4     // 不會卡死主線程 5     dispatch_async(dispatch_get_main_queue(), ^{ 6  7         NSLog(@"下載圖片1%@", [NSThread currentThread]); 8     }); 9     // 非同步函數 + 主隊列10     dispatch_async(dispatch_get_main_queue(), ^{11         NSLog(@"下載圖片2%@", [NSThread currentThread]);12     });13     // 非同步函數 + 主隊列14     dispatch_async(dispatch_get_main_queue(), ^{15         NSLog(@"下載圖片3%@", [NSThread currentThread]);16     });17  18 // 非同步函數 + 串列隊列19 // 開啟一條新線程.20 // 一個隊列對應一條線程.每一條線程中的任務都是按順序執行-- 串列執行.21 // 如果建立了多個串列隊列,會開啟多條線程.22 - (void)test123 {24     dispatch_queue_t queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);25     dispatch_queue_t queue1 = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);26     dispatch_async(queue, ^{27         NSLog(@"下載圖片1%@", [NSThread currentThread]);28     });29     dispatch_async(queue1, ^{30         NSLog(@"下載圖片2%@", [NSThread currentThread]);31     });32 33     dispatch_async(queue1, ^{34         NSLog(@"下載圖片3%@", [NSThread currentThread]);35     });36 37     dispatch_async(queue, ^{38         NSLog(@"下載圖片4%@", [NSThread currentThread]);39     });40 } 41  42 // 非同步函數 + 全域並發隊列43 // 開啟一條線程,在子線程中執行任務44 // 根據任務數量,開啟多條線程.45 // GCD自動幫我們開啟一定數量的線程.46 - (void)test247 {48     dispatch_queue_t queue = dispatch_get_global_queue(0, 0);49     // 非同步函數 + 全域並發隊列50     dispatch_async(queue, ^{51         NSLog(@"下載圖片1%@",[NSThread currentThread]);52     });53     // 非同步函數 + 全域並發隊列54     dispatch_async(queue, ^{55         NSLog(@"下載圖片2%@",[NSThread currentThread]);56     });57 }58  59     // 一次性代碼,只會執行一次,能夠保證安全執行緒的.60     // 線程第一次執行完任務之後,其他線程就不會再進來了.61     static dispatch_once_t onceToken;62     dispatch_once(&onceToken, ^{63         NSLog(@"%@",[NSThread currentThread]);64         NSLog(@"一次性代碼,只執行一次%@",string);65     });66  67    //隊列組的使用68 -(void)groupTest69 {70     __block UIImage *image1,*image2;71     // 建立一個隊列組72     dispatch_group_t group = dispatch_group_create();73     // 將我們需要執行的任務放在隊列中裡面74     // 非同步方法75     dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{76         // 任務177         NSLog(@"renwu 1");78         image1 = [self downloadImageWithUrlString:@"http://e.hiphotos.baidu.com/zhidao/pic/item/203fb80e7bec54e722c5454ebb389b504fc26ab0.jpg"];79     });80     // 再往隊列組中添加一個任務81     dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{82         // 任務283         NSLog(@"renwu 2");84         image2 = [self downloadImageWithUrlString:@"http://www.cnwan.com.cn/uploads/allimg/2011-07/01002414-1-10j14.jpg"];   85     });86    // 隊列組中的任務都執行完畢以後,會發送一個通知,執行下面的方法87     dispatch_group_notify(group, dispatch_get_main_queue(), ^{88         // 隊列組中的任務執行完畢之後的操作89         NSLog(@"renwu over");90        // 合并圖片91         self.imageView.image = [self BingImageWith:image1 :image2]; 92     });93 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.