GCD的基本使用代碼
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark --------------------
#pragma mark Events
/*
隊(duì)列,都是用來存放任務(wù)
并發(fā)隊(duì)列:允許多個(gè)任務(wù)同時(shí)執(zhí)行
1)直接create dispatch_queue_create
2)全局并發(fā)隊(duì)列
串行隊(duì)列:只能一個(gè)接著一個(gè)的執(zhí)行
1)直接create dispatch_queue_create
2)主隊(duì)列:
1)所有在主隊(duì)列中的任務(wù)都會被放在主線程中執(zhí)行
2)主隊(duì)列中的任務(wù)在執(zhí)行之前會先檢查主線程的狀態(tài),
如果發(fā)現(xiàn)主線程當(dāng)前正在執(zhí)行任務(wù)那么會暫停隊(duì)列中任務(wù)的調(diào)度
同步:必須要得到該方法的返回值才能夠繼續(xù)往下執(zhí)行--->如果我沒有執(zhí)行完畢,那么后面的將永遠(yuǎn)無法執(zhí)行
異步:可以繼續(xù)往下執(zhí)行,等前面的任務(wù)執(zhí)行完畢之后再回頭執(zhí)行-->我無所謂,你可以先執(zhí)行后面的代碼
*/
// 同步:只能在當(dāng)前線程中執(zhí)行任務(wù),不具備開啟新線程的能力
// 異步:可以在新的線程中執(zhí)行任務(wù),具備開啟新線程的能力
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 異步函數(shù)+主隊(duì)列
// [self asyncMian];
// 同步函數(shù)+主隊(duì)列
// [self syncMian];
// 異步函數(shù)+并發(fā)隊(duì)列
// [self asyncConCurrent];
// 異步函數(shù)+串行隊(duì)列
// [self asyncSerial];
// 同步函數(shù)+并發(fā)隊(duì)列
// [self syncConCurrent];
// 同步函數(shù)+串行
[self syncSerial];
}
#pragma mark --------------------
#pragma mark - Methods
// 異步函數(shù)+主隊(duì)列:不會開線程,所有的任務(wù)串行執(zhí)行,在主線程執(zhí)行
/*
1---<NSThread: 0x600000077f80>{number = 1, name = main}
2---<NSThread: 0x600000077f80>{number = 1, name = main}
3---<NSThread: 0x600000077f80>{number = 1, name = main}
4---<NSThread: 0x600000077f80>{number = 1, name = main}
*/
- (void)asyncMian {
// 獲得主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
// 封裝任務(wù)
dispatch_async(queue, ^{
NSLog(@"1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4---%@", [NSThread currentThread]);
});
}
// 同步函數(shù)+主隊(duì)列:
// 會死鎖
- (void)syncMian {
// 獲得主隊(duì)列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"%s",__func__);
// 封裝任務(wù)
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"5---%@",[NSThread currentThread]);
});
}
// 異步函數(shù)+并發(fā)隊(duì)列
/*
// 會不會開線程:會
// 如果開線程,那么開幾條?多條(并不是有幾個(gè)任務(wù)就開幾條線程)
// 隊(duì)列內(nèi)部任務(wù)如何執(zhí)行:并發(fā)
*/
/*
start------
end------
2---<NSThread: 0x600000074440>{number = 4, name = (null)}
1---<NSThread: 0x608000071040>{number = 3, name = (null)}
3---<NSThread: 0x600000076dc0>{number = 5, name = (null)}
*/
- (void)asyncConCurrent {
//1.創(chuàng)建隊(duì)列?
/*
第一個(gè)參數(shù):標(biāo)簽 隊(duì)列的名稱 C語言
第二個(gè)參數(shù):隊(duì)列的類型
*/
//dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_CONCURRENT);
// 獲得全局并發(fā)隊(duì)列
/*
第一個(gè)參數(shù):優(yōu)先級
第二個(gè)參數(shù):留給未來使用的0
*/
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(@"start------");
// 2.使用異步函數(shù)添加操作到隊(duì)列中
// 該方法完成了以下操作:1)封裝任務(wù) 2)把任務(wù)添加到隊(duì)列
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
NSLog(@"end------");
}
// 異步函數(shù)+串行隊(duì)列
/*
//會不會開線程:會
//如果開線程,那么開幾條?1
//隊(duì)列內(nèi)部任務(wù)如何執(zhí)行:串行
*/
/*
start------
end------
1---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
2---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
3---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
4---<NSThread: 0x60000026bb00>{number = 3, name = (null)}
*/
-(void)asyncSerial {
//1.創(chuàng)建隊(duì)列
/*
第一個(gè)參數(shù):標(biāo)簽 隊(duì)列的名稱 C語言
第二個(gè)參數(shù):隊(duì)列的類型
*/
dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_SERIAL);
NSLog(@"start------");
//2.使用異步函數(shù)添加操作到隊(duì)列中
//該方法完成了以下操作:1)封裝任務(wù) 2)把任務(wù)添加到隊(duì)列
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4---%@",[NSThread currentThread]);
});
NSLog(@"end------");
}
// 同步函數(shù)+并發(fā)隊(duì)列
/*
//會不會開線程:不會
//如果開線程,那么開幾條 X
//隊(duì)列內(nèi)部任務(wù)如何執(zhí)行:串行
*/
/*
start------
1---<NSThread: 0x600000063740>{number = 1, name = main}
2---<NSThread: 0x600000063740>{number = 1, name = main}
3---<NSThread: 0x600000063740>{number = 1, name = main}
4---<NSThread: 0x600000063740>{number = 1, name = main}
end------
*/
- (void)syncConCurrent {
//1.創(chuàng)建隊(duì)列
/*
第一個(gè)參數(shù):標(biāo)簽 隊(duì)列的名稱 C語言
第二個(gè)參數(shù):隊(duì)列的類型
*/
dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"start------");
//2.使用同步函數(shù)添加操作到隊(duì)列中
//該方法完成了以下操作:1)封裝任務(wù) 2)把任務(wù)添加到隊(duì)列
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4---%@",[NSThread currentThread]);
});
NSLog(@"end------");
}
// 同步函數(shù)+串行
/*
//會不會開線程:不會
//如果開線程,那么開幾條 X
//隊(duì)列內(nèi)部任務(wù)如何執(zhí)行:串行
*/
/*
start------
1---<NSThread: 0x60000007cb00>{number = 1, name = main}
2---<NSThread: 0x60000007cb00>{number = 1, name = main}
3---<NSThread: 0x60000007cb00>{number = 1, name = main}
4---<NSThread: 0x60000007cb00>{number = 1, name = main}
end------
*/
-(void)syncSerial
{
//1.創(chuàng)建隊(duì)列
/*
第一個(gè)參數(shù):標(biāo)簽 隊(duì)列的名稱 C語言
第二個(gè)參數(shù):隊(duì)列的類型
*/
dispatch_queue_t queue = dispatch_queue_create("com.seemygo.www.download", DISPATCH_QUEUE_SERIAL);
NSLog(@"start------");
//2.使用同步函數(shù)添加操作到隊(duì)列中
//該方法完成了以下操作:1)封裝任務(wù) 2)把任務(wù)添加到隊(duì)列
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3---%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"4---%@",[NSThread currentThread]);
});
NSLog(@"end------");
}
@end