IOS开发之GCD的基本知识
2019獨角獸企業重金招聘Python工程師標準>>>
GCD的基本知識
1.GCD純C語言,為多核的并行運算提出的,GCD會自動管理線程的生命周期,程序員只需要告訴GC執行什么任務,不需要編寫管理線程的代碼
2.GCD的核心概念:
a.定制任務
b.將任務添加到隊列中:GCD會自動的將隊列的任務取出,放到對應的線程中執行
任務的取出遵循隊列的先進先出原則(FIFO)
GCD執行任務的兩種方式:
a.同步方式執行任務,不會創建新的線程,就在當前線程中執行操作
b.異步方式執行任務,在另一個線程中執行任務
//隊列類型
//1.并發隊列:可以讓多個任務并發執行(自動開啟多個線程同時執行任務)
//并發功能只有在異步(dispatch_async)函數下才有效;
//2.串行隊列:一個接著一個執行;
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
#pragma mark - UITouch觸摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
?? ?
?? ?
? ? [self test9];
}
#pragma mark - GCD的基本使用
第一種方式串行+同步
- (void) test1{
?//1.創建一個串行隊列
? ? //dispatch_queue_t 隊列類型
? ? //參數1:隊列標識(隊列名),可以傳任意的C語言的字符串
? ? //參數2:隊列的類型(并發或者串行)
? ? //DISPATCH_QUEUE_SERIAL 串行
? ? //DISPATCH_QUEUE_CONCURRENT 并發
dispatch_queue_t queue = dispatch_queue_create("abc,",DISPATCH_QUEUE_SERIAL);
//2.將一個任務添加到隊列中
? ?dispatch_sync(queue,^{
? ? ? //這里是線程執行的操作?
? ?[self longtimeOperation:0];?
});
? ? //同步添加
? ? //參數1:隊列 參數2:任務
//? ? dispatch_sync(queue, ^{
// ? ? ? ?
//? ? ? ? [self longtimeOperation:0];
//? ? });
?? ?
?? //如果想將多個任務添加到隊列中
? ? for (int i=0; i<10;i++) {
?? ? ? ?
? ? ? ? dispatch_sync(queue, ^{
?? ? ? ? ? ?
? ? ? ? ? ? [self longtimeOperation:i];
?? ? ? ?
? ? ? ? });
? ? }
?
}
//第二種方式串行異步:在其他線程中執行,開啟新的線程
//在子線程中任務一個一個地執行
- (void) test2{
?? ?
? ? //1.創建隊列
? ? dispatch_queue_t queue = dispatch_queue_create("abc", NULL);
?
? ? //2.添加到隊列
?? ?
? ? for (int i=0; i<10; i++) {
? ? ? ? dispatch_async(queue, ^{
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
? ? }
?? ?
? ?
}
//第三種方式并發同步
//在當前進程中同時執行
- (void) test3{
?? ?
? ? //1.創建隊列
? ? dispatch_queue_t queue = dispatch_queue_create("abc", DISPATCH_QUEUE_CONCURRENT);
?? ?
? ? //2.添加到隊列
? ? for (int i = 0; i<10; i++) {
? ? ? ? dispatch_sync(queue, ^{
?? ? ? ? ? ?
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
? ? }
?? ?
}
//第四種方式并發異步
//多個任務在多個線程中同時執行
- (void)test4{
?? ?
? ? dispatch_queue_t queue = dispatch_queue_create("abc", DISPATCH_QUEUE_CONCURRENT);
?? ?
? ? for(int i=0;i<10;i++){
?? ? ? ?
? ? ? ? dispatch_async(queue, ^{
?? ? ? ? ? ?
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
?? ? ? ?
? ? }
?? ?
}
#pragma mark - 全局隊列
//全局隊列:系統已經創建好的全局隊列
//使用的時候直接去取出來就可以
- (void)test5{
? ?
? ?
?//1.拿到全局隊列是一個并發隊列
? ? //參數1:隊列的優先級 0
? ? //參數2:預留參數
? ? dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
?? ?
? ? //2.添加任務,異步執行
? ? for (int i =0;i<10; i++) {
? ? ? ? dispatch_async(queue, ^{
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
? ? }
}
#pragma mark - 主隊列
//主隊列:是系統創建好專門用來存儲在主線程中執行任務的隊列;
//添加到主隊列中的任務只能在主線程中執行的,使用的時候只需要直接去拿就可以了;
//結果:死鎖
//
- (void)test7{
? ? //1.拿到主隊列
? ? dispatch_queue_t queue = dispatch_get_main_queue();
?? ?
? ? //2.將任務添加到主隊列中,異步執行
?? ?
? ? //2.添加任務,同步執行
? ? for (int i =0;i<10; i++) {
? ? ? ? dispatch_sync(queue, ^{
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
? ? }
}
//主隊列:
//異步執行:在其他線程中執行.需要開啟新的線程;
- (void) test6{
?? ?
? ? //1.拿到主隊列
? ? dispatch_queue_t queue = dispatch_get_main_queue();
?? ?
? ? //2.將任務添加到主隊列中,異步執行
? ? //2.添加任務,異步執行
? ? for (int i =0;i<10; i++) {
? ? ? ? dispatch_async(queue, ^{
? ? ? ? ? ? [self longtimeOperation:i];
? ? ? ? });
? ? }
?? ?
}
#pragma mark - 線程之間的通信
- (void) test8{
? ? //自己的子線程
? ? dispatch_async(dispatch_get_global_queue(0, 0), ^{
?? ? ? ?
? ? ? ? NSLog(@"%@",[NSThread currentThread]);
?? ? ? ?
? ? ? ? //NSURL url = []
?? ? ? ?
? ? ? ? UIImage *image = [self downLoadImagewithUrl:@""];
?? ? ? ?
? ? ? ? //將圖片傳回主線程
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? //在主隊列中需要執行的任務
?? ? ? ? ? ?
? ? ? ? ? ? _imageView.image = image;
?? ? ? ? ? ?
? ? ? ? });
?? ? ? ?
? ? });
?? ?
?? ?
? ?
?? ?
}
- (UIImage *)downLoadImagewithUrl:(NSString *)path{
?? ?
? ? //1.創建URL
? //? NSURL *url = [NSURL URLWithString:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fw%253D580%2Fsign%3Da7bfe99adbf9d72a17641015e42b282a%2F5dfed5628535e5dd5ae2287a74c6a7efcf1b6201.jpg&thumburl=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D3096927341%2C3524930798%26fm%3D21%26gp%3D0.jpg"];
? ? NSURL *url = [NSURL URLWithString:path];
? ? NSData *data = [[NSData alloc]initWithContentsOfURL:url];
? ? UIImage *image = [UIImage imageWithData:data];
? ? return image;
}
}
#pragma mark - 組隊列
//用來存放隊列
//多個任務在不同的線程中需要異步執行,全部執行完畢后回到主線程
- (void)test9{
?? ?
? ? //1.創建一個隊列組,用來存放隊列的;
? ? dispatch_group_t group = dispatch_group_create();
?? ?
? ? //參數1:隊列組
? ? //參數2:隊列
? ? //參數3:任務
? ? //2.將任務添加到隊列組的隊列中
? ? dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
?? ? ? ?
? ? ? ? [self downLoadImagewithUrl:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fw%253D580%2Fsign%3Da7bfe99adbf9d72a17641015e42b282a%2F5dfed5628535e5dd5ae2287a74c6a7efcf1b6201.jpg&thumburl=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D3096927341%2C3524930798%26fm%3D21%26gp%3D0.jpg"];
? ? ? ? NSLog(@"下載圖片1");
?? ? ? ?
? ? });
?? ?
? ? dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
?? ? ? ?
? ? ? ? [self downLoadImagewithUrl:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201407%2F09%2F20140709133734_sc8FR.jpeg&thumburl=http%3A%2F%2Fimg5.imgtn.bdimg.com%2Fit%2Fu%3D3045961850%2C3296329825%26fm%3D21%26gp%3D0.jpg"];
? ? ? ? NSLog(@"下載圖片2");
? ? });
?
?? ?
? ? //3.所有任務執行完畢后回到主線程
? ? //參數1:哪個組的任務全部完成
? ? //參數2:組的任務完成后執行哪個隊列的任務
? ? dispatch_group_notify(group,dispatch_get_main_queue(), ^{
? ? ? ? NSLog(@"回到了主線程");
? ? ? ? NSLog(@"%@",[NSThread currentThread]);
? ? });
?? ?
}
#pragma mark - 任務
-(void)longtimeOperation:(int) i{
//這是一個任務,拿到當前線程并打印
NSLog(@"%@,%d",[NSThread currentThread],i);
}
轉載于:https://my.oschina.net/luhoney/blog/666627
總結
以上是生活随笔為你收集整理的IOS开发之GCD的基本知识的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将字符串截取返回
- 下一篇: 我这样理解技术人的成长过程