標籤: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