IOS GCD(線程的 串列、並發 基本使用)

來源:互聯網
上載者:User

標籤:cto   patch   ase   ios   class   style   pre   thread   功能   

// 凡是函數名種帶有create\copy\new\retain等字眼, 都需要在不需要使用這個資料的時候進行release// GCD的資料類型在ARC環境下不需要再做release// CF(Core Foundation)的資料類型在ARC環境下還是需要再做release
@implementation HMViewController- (void)viewDidLoad{    [super viewDidLoad];        [self performSelectorInBackground:@selector(test) withObject:nil];    //    [self syncMainQueue];}- (void)test{    NSLog(@"test --- %@", [NSThread currentThread]);        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"任務 --- %@", [NSThread currentThread]);    });}/** * 使用dispatch_async非同步函數, 在主線程中往主隊列中新增工作 */- (void)asyncMainQueue{    // 1.獲得主隊列    dispatch_queue_t queue = dispatch_get_main_queue();        // 2.新增工作到隊列中 執行    dispatch_async(queue, ^{        NSLog(@"----下載圖片1-----%@", [NSThread currentThread]);    });}/** * 使用dispatch_sync同步函數, 在主線程中往主隊列中新增工作 : 任務無法往下執行 */- (void)syncMainQueue{    // 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]);//    });        // 不會開啟新的線程, 所有任務在主線程中執行}// 凡是函數名種帶有create\copy\new\retain等字眼, 都需要在不需要使用這個資料的時候進行release// GCD的資料類型在ARC環境下不需要再做release// CF(Core Foundation)的資料類型在ARC環境下還是需要再做release/** * 用dispatch_sync同步函數往串列列中新增工作 */- (void)syncSerialQueue{    // 1.建立串列隊列    dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", NULL);        // 2.新增工作到隊列中 執行    dispatch_sync(queue, ^{        NSLog(@"----下載圖片1-----%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"----下載圖片2-----%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"----下載圖片3-----%@", [NSThread currentThread]);    });        // 3.釋放資源//    dispatch_release(queue);   // MRC(非ARC)        // 總結: 不會開啟新的線程}/** * 用dispatch_sync同步函數往並發隊列中新增工作 */- (void)syncGlobalQueue{    // 1.獲得全域的並發隊列    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);        // 2.新增工作到隊列中 執行    dispatch_sync(queue, ^{        NSLog(@"----下載圖片1-----%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"----下載圖片2-----%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"----下載圖片3-----%@", [NSThread currentThread]);    });        // 總結: 不會開啟新的線程, 並發隊列失去了並發的功能}/** * 用dispatch_async非同步函數往串列隊列中新增工作 */- (void)asyncSerialQueue{    // 1.建立串列隊列    dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", NULL);        // 2.新增工作到隊列中 執行    dispatch_async(queue, ^{        NSLog(@"----下載圖片1-----%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"----下載圖片2-----%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"----下載圖片3-----%@", [NSThread currentThread]);    });        // 總結: 只開1個線程執行任務}/** * 用dispatch_async非同步函數往並發隊列中新增工作 */- (void)asyncGlobalQueue{    // 1.獲得全域的並發隊列    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);        // 2.新增工作到隊列中 執行    dispatch_async(queue, ^{        NSLog(@"----下載圖片1-----%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"----下載圖片2-----%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"----下載圖片3-----%@", [NSThread currentThread]);    });        // 總結: 同時開啟了3個線程}@end

 

IOS GCD(線程的 串列、並發 基本使用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.