GCD的知識(shí)技能點(diǎn)截圖:

Snip20160727_1.png
知識(shí)點(diǎn)逐一簡介
NO.1 GCD同步函數(shù)+串行隊(duì)列(所有的都寫為一個(gè)小方法,調(diào)用就好)
-(void)syncSerial
{
//1.創(chuàng)建串行隊(duì)列
#"45":隊(duì)列標(biāo)簽可以隨便命名,但最好還是和自己要執(zhí)行的任務(wù)有關(guān)(見名知意)
#DISPATCH_QUEUE_SERIAL:表示為串行隊(duì)列的宏
dispatch_queue_t queue = dispatch_queue_create("45", DISPATCH_QUEUE_SERIAL);
//2.封裝任務(wù)(其實(shí)就是把自己要執(zhí)行的任務(wù)放入創(chuàng)建好的隊(duì)列中)
# dispatch_sync:為同步函數(shù),不具備開線程能力
dispatch_sync(queue, ^{
//打印一句話和當(dāng)前線程()
NSLog(@"演員1-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員2-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員3-----%@",[NSThread currentThread]);
});
}
#注:這樣執(zhí)行的三個(gè)任務(wù)都在主線程中執(zhí)行且是串行執(zhí)行的(即依次進(jìn)行)
NO.2 GCD同步函數(shù)+并行隊(duì)列
-(void)syncConcurrent
{
//1.創(chuàng)建隊(duì)列(這個(gè)隊(duì)列是自己創(chuàng)建的)
#DISPATCH_QUEUE_CONCURRENT:并發(fā)隊(duì)列的宏
dispatch_queue_t queue = dispatch_queue_create( "126", DISPATCH_QUEUE_CONCURRENT);
//2.封裝任務(wù)
dispatch_sync(queue, ^{
//打印一句話和當(dāng)前線程
NSLog(@"演員1-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員2-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員3-----%@",[NSThread currentThread]);
});
}
#注:不會(huì)開辟線程,在主線程中執(zhí)行任務(wù),任務(wù)是串行執(zhí)行的
#獲得系統(tǒng)的全局并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
#DISPATCH_QUEUE_PRIORITY_DEFAULT:隊(duì)列優(yōu)先級(jí)的宏
NO.3 GCD異步函數(shù)+串行隊(duì)列
-(void)asyncSerial
{
//1.創(chuàng)建串行隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("12345", DISPATCH_QUEUE_SERIAL);
//2.封裝任務(wù)(異步函數(shù))
# dispatch_async:異步函數(shù)具備開線程的能力
dispatch_async(queue, ^{
//打印一句話和當(dāng)前線程(打印出來number = 2,是在子線程中執(zhí)行的)
NSLog(@"丑八怪啊-1-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"丑八怪啊-2-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"丑八怪啊-3-----%@",[NSThread currentThread]);
});
}
#注:會(huì)開線程,開一條線程,隊(duì)列中的任務(wù)是依次執(zhí)行的
NO.4 GCD異步函數(shù)+并發(fā)隊(duì)列
-(void)asyncConcurrent
{
//1.創(chuàng)建并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_queue_create( "123456", DISPATCH_QUEUE_CONCURRENT);
//2.封裝任務(wù)(異步函數(shù))
dispatch_async(queue, ^{
//打印一句話和當(dāng)前線程
NSLog(@"我愛你1-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"我愛你2-----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"我愛你3-----%@",[NSThread currentThread]);
});
#注:會(huì)開啟多條線程,隊(duì)列中的任務(wù)是并發(fā)執(zhí)行的
}
NO.5 GCD同步函數(shù)+主隊(duì)列
-(void)syncMainQueue
{
//1.獲得主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
//2.封裝任務(wù)
dispatch_sync(queue, ^{
//打印一句話和當(dāng)前線程()
NSLog(@"演員1-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員2-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員3-----%@",[NSThread currentThread]);
});
}
#注:在主線程中執(zhí)行,不會(huì)開線程
NO.6 GCD異步函數(shù)+主隊(duì)列
-(void)asyncMainQueue
{
//1.獲得主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
//2.封裝任務(wù)
dispatch_async(queue, ^{
//打印一句話和當(dāng)前線程()
NSLog(@"演員1-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員2-----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"演員3-----%@",[NSThread currentThread]);
});
}
#注:所有任務(wù)都在主線程中執(zhí)行,不會(huì)開線程
GCD的常用函數(shù)
NO.1 延遲函數(shù)
//1.點(diǎn)擊屏幕調(diào)用GCD方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//GCD里面的延遲方法
[self delay];
}
//GCD里面的延遲方法
-(void)delay
{
//獲得全局并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//GCD延遲函數(shù)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
[self task];
});
}
//提供一個(gè)延遲調(diào)用的方法
-(void)task
{
NSLog(@"我愛你---%@",[NSThread currentThread]);
}
NO.2 柵欄函數(shù)
#注:柵欄函數(shù)不能用在全局并發(fā)隊(duì)列中,只能自己創(chuàng)建并發(fā)隊(duì)列
//點(diǎn)擊屏幕觸發(fā)方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.創(chuàng)建并發(fā)隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("download", DISPATCH_QUEUE_CONCURRENT);
//2.異步函數(shù)
//異步函數(shù)
dispatch_async(queue, ^{
NSLog(@"丑八怪1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"丑八怪2---%@",[NSThread currentThread]);
});
#實(shí)現(xiàn)需求:我們?cè)?,2都執(zhí)行完,才執(zhí)行3(就可以用柵欄函數(shù),攔截一下)
dispatch_barrier_async(queue, ^{
NSLog(@"------------------------------");
});
dispatch_async(queue, ^{
NSLog(@"丑八怪3---%@",[NSThread currentThread]);
});
}
#注:柵欄函數(shù)用來控制并發(fā)隊(duì)列中的執(zhí)行順序
GCD線程間的通信
//點(diǎn)擊屏幕下載一張圖片(準(zhǔn)備工作:在stroyboard拖一個(gè)UIImageView出來,把屬性拖出來)
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
#下載圖片是一個(gè)耗時(shí)操作,我們放在子線程中去執(zhí)行
#創(chuàng)建子線程下載圖片,用GCD的異步函數(shù)(這里只有一個(gè)任務(wù)用串行和并發(fā)的都行)
dispatch_queue_t queue = dispatch_queue_create("123", DISPATCH_QUEUE_CONCURRENT);//并發(fā)隊(duì)列
//這段代碼是在子線程中執(zhí)行的
dispatch_async(queue, ^{
//下載圖片
//1.確定url
NSURL *url = [NSURL URLWithString:@"http://img2.3lian.com/2014/f4/51/d/13.jpg"];
//1.2 下載二進(jìn)制數(shù)據(jù)到本地
NSData *imageData = [NSData dataWithContentsOfURL:url];
//1.3 將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為圖片
UIImage *image = [UIImage imageWithData:imageData];
//打印一下當(dāng)前線程
NSLog(@"--%@",[NSThread currentThread]);
# 現(xiàn)在圖片有了,需要更新UI,就得回到主線程中
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"***%@",[NSThread currentThread]);
});
});
}
}
#GCD中的線程通信函數(shù)是可以嵌套的