隊列 & 線程的執行方式(任務)
任務:block,消息等函數塊,他是由線程執行的。
線程執行任務的方式:
同步線程:當前線程a只能執行完事件A才能執行任務B。
異步線程:當前線程a執行A的時候又開了個線程b,執行任務B
隊列:存儲任務
串行隊列:一個任務(block)接着一個任務的出隊。通常由一個線程處理這一個隊列
併發隊列:一次有多個任務出隊,由多個線程處理。使用異步線程執行。
併發
2.GCD:
線程執行方式:
同步:dispatch_sync
異步:dispatch_async
隊列:
串行:串行隊列,dispatch_queue_create,主隊列dispatch_get_main_queue
併發:全局隊列,dispatch_get_global_queue異步
mainThread <--->mainQueue
async
3.GCD線程間通訊圖:
函數
4.url
dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"download:%@", [NSThread currentThread]); NSURL *url = [NSURL URLWithString:@"http://pic.nipic.com/2007-12-06/2007126205543511_2.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; NSLog(@"image:%@", image); //通知主線程更新 UI dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"updateUI:%@", [NSThread currentThread]); self.imgView.image = image; }); });
outputspa
2016-03-03 12:40:00.368 gcdThread[8835:87857] download:<NSThread: 0x7fa57b532800>{number = 2, name = (null)} 2016-03-03 12:40:00.952 gcdThread[8835:87857] image:<UIImage: 0x7fa57d80d0c0>, {1024, 375} 2016-03-03 12:40:00.952 gcdThread[8835:87600] updateUI:<NSThread: 0x7fa57b605170>{number = 1, name = main}
5.GCD 隊列組
串行隊列中,任務執行時一個接着一個的。任務間通訊是線性的。B任務能夠等待A任務完成後拿到A任務的數據。
在並行隊列中,任務執行時異步的。須要靠通知(線程間提早作好約定,當這個約定實現了,纔會傳遞數據)。dispatch_group 組操做做爲任務之間通訊的一種約定
.net
- (void) groupMultiDownload { __block UIImage *img1, *img2; dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(0, 0); dispatch_group_async(group, queue, ^{ NSURL *url1 = [NSURL URLWithString:@"http://pic8.nipic.com/20100731/2531170_101711635627_2.jpg"]; NSData *data1 = [NSData dataWithContentsOfURL:url1]; NSLog(@"download1:%@", [NSThread currentThread]); img1 = [UIImage imageWithData:data1]; }); dispatch_group_async(group, queue, ^{ NSURL *url2 = [NSURL URLWithString:@"http://pic9.nipic.com/20100813/2531170_083235193107_2.jpg"]; NSLog(@"download2:%@", [NSThread currentThread]); NSData *data2 = [NSData dataWithContentsOfURL:url2]; img2 = [UIImage imageWithData:data2]; }); //隊列組中全部任務完成後 執行 notify dispatch_group_notify(group, queue, ^{ NSLog(@"download finish:%@", [NSThread currentThread]); UIGraphicsBeginImageContextWithOptions(img1.size, NO, 0.0); [img1 drawInRect:CGRectMake(0, 0, img1.size.width, img1.size.height)]; [img2 drawInRect:CGRectMake(0, 0, img2.size.width, img2.size.height)]; UIImage *fullImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); dispatch_async(dispatch_get_main_queue(), ^{ _imgView.image = fullImg; }); }); }
6.延遲執行線程
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{ NSLog(@"------task------%@", [NSThread currentThread]); });//使用的是 全局併發隊列,因此會自動開啓一個子線程,執行block中的內容
7.代碼一次執行:代碼在程序運行階段只會被執行一次code
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"----once"); HMImageDownloader *downloader = [[HMImageDownloader alloc] init]; [downloader download]; });
PS:使用 gcd實現單例模式。
blog