1 // 2 // ViewController.m 3 // 02-GCD基本使用 4 // 5 // Created by mac on 16/4/21. 6 // Copyright © 2016年 mac. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 } 20 21 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 22 23 //多線程正常開啓 24 [self asyncConcurrentQueue]; //1. 異步併發 25 [self asyncSerialQueue]; //2. 異步串行 26 27 //結果都是-主線程在執行 28 [self syncConcurrentQueue]; //3. 同步併發 29 [self syncSerialQueue];//4. 同步串行 30 [self mainQueue]; //5. 主隊列 31 32 //死鎖 33 [self syncMainQueue]; //6. 同步主線程 34 } 35 36 /** 37 * async -- 主隊列(很經常使用) 38 * 線程之間通訊比較常使用 39 */ 40 - (void)mainQueue { 41 42 //1. 主隊列(添加到主隊列中的任務,都會自動放到主線程中執行) 43 dispatch_queue_t queue = dispatch_get_main_queue(); 44 45 //2.添加任務到主隊列中異步執行 46 dispatch_async(queue, ^{ 47 48 NSLog(@"1111==%@", [NSThread currentThread]); 49 }); 50 dispatch_async(queue, ^{ 51 52 NSLog(@"2222==%@", [NSThread currentThread]); 53 }); 54 dispatch_async(queue, ^{ 55 56 NSLog(@"3333==%@", [NSThread currentThread]); 57 }); 58 dispatch_async(queue, ^{ 59 60 NSLog(@"4444==%@", [NSThread currentThread]); 61 }); 62 dispatch_async(queue, ^{ 63 64 NSLog(@"5555==%@", [NSThread currentThread]); 65 }); 66 67 } 68 /** 69 * syncMain 主隊列同步(不能用) 70 */ 71 - (void)syncMainQueue { 72 73 NSLog(@"syncMain====="); 74 75 //1. 主隊列(添加到主隊列中的任務,都會自動放到主線程中執行) 76 dispatch_queue_t queue = dispatch_get_main_queue(); 77 78 //2.添加任務到主隊列中同步執行 79 dispatch_sync(queue, ^{ 80 81 //等待中,產生死鎖 82 NSLog(@"1111==%@", [NSThread currentThread]); 83 }); 84 dispatch_sync(queue, ^{ 85 86 NSLog(@"2222==%@", [NSThread currentThread]); 87 }); 88 dispatch_sync(queue, ^{ 89 90 NSLog(@"3333==%@", [NSThread currentThread]); 91 }); 92 dispatch_sync(queue, ^{ 93 94 NSLog(@"4444==%@", [NSThread currentThread]); 95 }); 96 dispatch_sync(queue, ^{ 97 98 NSLog(@"5555==%@", [NSThread currentThread]); 99 }); 100 101 //互相等待 102 NSLog(@"========syncMain====="); 103 } 104 105 106 /** 107 * async--併發隊列(最經常使用) 108 * 會建立線程:同時開多條 109 * 任務的執行方式:併發執行 110 */ 111 - (void)asyncConcurrentQueue { 112 113 //獲取併發的全局隊列 114 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 115 116 //將 任務 添加到 全局隊列 中去 異步執行 117 dispatch_async(queue, ^{ 118 119 NSLog(@"1111==%@", [NSThread currentThread]); 120 }); 121 dispatch_async(queue, ^{ 122 123 NSLog(@"2222==%@", [NSThread currentThread]); 124 }); 125 dispatch_async(queue, ^{ 126 127 NSLog(@"3333==%@", [NSThread currentThread]); 128 }); 129 dispatch_async(queue, ^{ 130 131 NSLog(@"4444==%@", [NSThread currentThread]); 132 }); 133 dispatch_async(queue, ^{ 134 135 NSLog(@"5555==%@", [NSThread currentThread]); 136 }); 137 } 138 139 /** 140 * async--串行隊列(有時候用) 141 * 會建立線程:只開一條線程 142 * 任務的執行方式:(順序執行) 143 */ 144 - (void)asyncSerialQueue { 145 146 //1. 建立一個串行隊列 147 dispatch_queue_t queue = dispatch_queue_create("隨便寫", NULL); 148 149 //1. 將任務 添加到 串行隊列中 去異步執行 150 dispatch_async(queue, ^{ 151 152 NSLog(@"1111==%@", [NSThread currentThread]); 153 }); 154 dispatch_async(queue, ^{ 155 156 NSLog(@"2222==%@", [NSThread currentThread]); 157 }); 158 dispatch_async(queue, ^{ 159 160 NSLog(@"3333==%@", [NSThread currentThread]); 161 }); 162 dispatch_async(queue, ^{ 163 164 NSLog(@"4444==%@", [NSThread currentThread]); 165 }); 166 dispatch_async(queue, ^{ 167 168 NSLog(@"5555==%@", [NSThread currentThread]); 169 }); 170 } 171 172 /** 173 * sync--併發隊列 174 * 不會建立線程:不開新線程,矛盾了 175 * 任務執行方式:串行執行(一個任務執行完,再執行下一個任務) 176 */ 177 - (void)syncConcurrentQueue { 178 179 //獲取併發的全局隊列 180 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 181 182 //將 任務 添加到全局隊列中去 同步執行 183 dispatch_sync(queue, ^{ 184 185 NSLog(@"1111==%@", [NSThread currentThread]); 186 }); 187 dispatch_sync(queue, ^{ 188 189 NSLog(@"2222==%@", [NSThread currentThread]); 190 }); 191 dispatch_sync(queue, ^{ 192 193 NSLog(@"3333==%@", [NSThread currentThread]); 194 }); 195 dispatch_sync(queue, ^{ 196 197 NSLog(@"4444==%@", [NSThread currentThread]); 198 }); 199 dispatch_sync(queue, ^{ 200 201 NSLog(@"5555==%@", [NSThread currentThread]); 202 }); 203 } 204 205 /** 206 * sync--串行隊列(不怎麼用) 207 * 不會建立線程:不開新線程 208 * 任務執行方式:串行執行(一個任務執行完,再執行下一個任務) 209 */ 210 - (void)syncSerialQueue { 211 212 //1. 建立一個串行隊列 213 dispatch_queue_t queue = dispatch_queue_create("隨便寫", NULL); 214 215 //將 任務 添加到串行隊列中去 同步執行 216 dispatch_sync(queue, ^{ 217 218 NSLog(@"1111==%@", [NSThread currentThread]); 219 }); 220 dispatch_sync(queue, ^{ 221 222 NSLog(@"2222==%@", [NSThread currentThread]); 223 }); 224 dispatch_sync(queue, ^{ 225 226 NSLog(@"3333==%@", [NSThread currentThread]); 227 }); 228 dispatch_sync(queue, ^{ 229 230 NSLog(@"4444==%@", [NSThread currentThread]); 231 }); 232 dispatch_sync(queue, ^{ 233 234 NSLog(@"5555==%@", [NSThread currentThread]); 235 }); 236 } 237 238 @end
//非ARC --須要釋放建立的隊列 // dispatch_release(queue); /** * Foundation:OC * Core Foundation:C * Foundation和Core Foundation框架的數據類型能夠互相轉換 */ NSString *str = @"1234"; CFStringRef str2 = (__bridge CFStringRef)str; NSString *str3 = (__bridge NSString *)str2; NSLog(@"%@=%@=%@", str, str2, str3);
橋接字符串多線程