GCD的基本使用

时间:2021-08-12 05:18:28

GCD全称Grand Central Dispatch,是Apple提供的一套底层API,提供了一种新的方法来进行并发程序编写,它的API包含在libdispatch库中.

觉得需要理解GCD中的三个要点:

1.同步异步(sync,async)

sync表示同步,不会开启新线程,任务是在当前线程中执行; async表示异步,具备开启新线程的能力,具体是否开启新线程需要看队列,比如:全局队列,串型队列会开启新线程,主队列就不会开启

2.队列

串型队列:

dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL)

 

并发队列:

dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ,其中第一个参数目前有四种选择:

#define DISPATCH_QUEUE_PRIORITY_HIGH 2 优先级搞
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认级别 一般开发采用
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) 优先级低
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 优先级最低

 

主队列:

dispatch_get_main_queue()

 

3.任务

GCD中执行的任务,需要执行的代码写入此处就OK,GCD任务一般情况下都是在block中.

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

// [self asyncConcurrent];
[self asyncSerial];
// [self asyncMain];

// [self syncMain];
// [self syncSerial];
// [self syncConcurrent];

}

- (void)syncMain{
// 线程阻塞
dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)syncSerial{
// 在当前线程中顺序执行任务
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)syncConcurrent{
// 在当前线程中顺序执行任务
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_sync(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}



- (void)asyncMain{
// Main线程中异步执行任务
dispatch_queue_t queue = dispatch_get_main_queue();

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)asyncSerial{
// 开启一条新线程顺序执行任务
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_SERIAL);

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}

- (void)asyncConcurrent{
// 开启多条新线程顺序执行任务
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"i=%ld %@",i,[NSThread currentThread]);
}
});

NSLog(@"%s----------%@",__func__,[NSThread currentThread]);
}