iOS多線程開發小demo6 GCD

來源:互聯網
上載者:User

標籤:class   blog   code   http   com   get   

//  DYFViewController.m//  623-07-GCD////  Created by dyf on 14-6-23.//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.//#import "DYFViewController.h"@interface DYFViewController ()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@end@implementation DYFViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.                    /*    // 非同步:具備開啟新線程的能力    dispatch_async(<#dispatch_queue_t queue#>, ^{        <#code#>    });    // 同步:不具備開啟新線程的能力    dispatch_sync(<#dispatch_queue_t queue#>, ^{        <#code#>    })     */}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self testBackToMain];}- (void)testBackToMain {    // 擷取全域並發隊列    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    // 非同步隊列    dispatch_async(queue, ^{        NSLog(@"-----%@", [NSThread currentThread]);        // 下載圖片        NSString *path = @"http://image.cache.xiu8.com/live/125/125/997729.jpg";        NSURL *url = [NSURL URLWithString:path];        NSData *data = [NSData dataWithContentsOfURL:url];                UIImage *image = [UIImage imageWithData:data];        // 回到主線程顯示圖片        dispatch_async(dispatch_get_main_queue(), ^{            NSLog(@"-----------%@", [NSThread currentThread]);            self.iconView.image = image;        });    });}- (void)testOnce {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"once");    });}- (void)testDelay {    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"222");    });}/** *  使用dispatch_async非同步函數,在主線程中網主隊列中新增工作 */- (void)testAsyncMainQueue {    // 1.獲得主隊列    dispatch_queue_t queue = dispatch_get_main_queue();    // 2.新增工作到隊列中,執行任務    dispatch_async(queue, ^{        NSLog(@"---------1-----%@", [NSThread currentThread]);    });    // 總結:不開新線程    }/** *  使用dispatch_sync同步函數,在主線程中網主隊列中新增工作,死:任務無法往下執行 */- (void)testSyncMainQueue {    // 1.獲得主隊列    dispatch_queue_t queue = dispatch_get_main_queue();    // 2.新增工作到隊列中,執行任務    dispatch_sync(queue, ^{        NSLog(@"---------1-----%@", [NSThread currentThread]);    });        // 總結:不開新線程,所有任務在主線程中串列執行}// 凡是函數名中帶有create、copy、new、retain等字眼,都需要在不需要使用這個資料的時候進行release// GCD的資料類型在ARC環境下不需要再做release// CF(Core Foundation)的資料類型在ARC環境下仍然要做release- (void)testCF {    CFArrayRef array = CFArrayCreate(NULL, NULL, 11, NULL);    CFRelease(array);}/** *  用dispatch_sync同步函數往串列隊列中新增工作 */- (void)testSyncSerialQueue {    // 1.建立串列隊列    dispatch_queue_t queue = dispatch_queue_create("cn.dongyue.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.釋放(MRC)    //dispatch_release(queue);            // 總結:不會開新的線程}/** *  用dispatch_sync同步函數往並發隊列中新增工作 */- (void)testSyncGlobalQueue {    // 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)testAsyncSerialQueue {    // 1.建立串列隊列    dispatch_queue_t queue = dispatch_queue_create("cn.dongyue.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)testAsyncGlobalQueue {    // 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

 

聯繫我們

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