沒有開啓新的線程, 任務是逐個執行的併發
// 獲取全局隊列 // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 建立併發隊列 dispatch_queue_t queue = dispatch_queue_create("myQueur", DISPATCH_QUEUE_CONCURRENT); // 在隊列裏添加任務 // 同步任務 dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----2------%@", [NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----3------%@", [NSThread currentThread]); } });
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main} GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
開啓了新線程, 任務是併發的異步
// 獲取全局隊列 // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 建立併發隊列 dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_CONCURRENT); // 異步任務 dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----2------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----3------%@", [NSThread currentThread]); } });
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)} GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)} GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)} GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)} GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)} GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)} GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)} GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)} GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)} GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)} GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)} GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)} GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)} GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)} GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
沒有開啓新的線程, 任務是逐個執行的async
// 建立併發隊列 dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL); // 同步任務 dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----2------%@", [NSThread currentThread]); } }); dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----3------%@", [NSThread currentThread]); } });
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main} GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
開啓新的線程, 任務是逐個執行的spa
// 建立串行隊列 dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL); // 異步任務 dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----2------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----3------%@", [NSThread currentThread]); } });
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)} GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
會形成死鎖的線程【不能在主隊列裏添加同步任務】線程
// 得到主隊列 dispatch_queue_t queue = dispatch_get_main_queue(); // 同步任務 dispatch_sync(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } });
沒有開啓新的線程, 任務是逐個執行的code
// 得到主隊列 dispatch_queue_t queue = dispatch_get_main_queue(); // 異步任務 dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----1------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----2------%@", [NSThread currentThread]); } }); dispatch_async(queue, ^{ for (int i = 0; i < 5; i++) { NSLog(@"----3------%@", [NSThread currentThread]); } });
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main} GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}