標籤: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(線程的 串列、並發 基本使用)