队列类型
并发队列(Concurrent Dispatch Queue)任务并发(同步)执行
串行队列(Serial Dispatch Queue)任务顺序执行
全局队列(Global Queue)并发队列
主队列(Main Queue)串行队列
/**
* lable : 队列名称
* attr : 队列类型,并发(ConCurrent),串行(Serial 或 NULL)
*/
dispatch_queue_t conCurrentQueue = dispatch_queue_create("conCurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t serialQueue1 = dispatch_queue_create("serial1", NULL);
/**
* 全局并发队列
* identifier : 优先级
* flags : 设置为0即可
*/
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
/**
* 主队列,串行队列
*/
dispatch_queue_t mainQueue = dispatch_get_main_queue();
执行方式
同步执行-在当前线程执行
异步执行-在新线程执行
queue:队列
block:任务,需要进行的操作
//同步任务
dispatch_sync(<#dispatch_queue_t queue#>, <#^(void)block#>)
//异步任务
dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)
代码示例
- 异步任务 + 串行队列
开启新线程,串行执行任务。
dispatch_queue_t serialCurrentQueue = dispatch_queue_create("myQueue", NULL);
dispatch_async(serialCurrentQueue, ^{
NSLog(@"串行队列-异步任务1-%@", [NSThread currentThread]);
});
dispatch_async(serialCurrentQueue, ^{
NSLog(@"串行队列-异步任务2-%@", [NSThread currentThread]);
});
dispatch_async(serialCurrentQueue, ^{
NSLog(@"串行队列-异步任务3-%@", [NSThread currentThread]);
});
dispatch_async(serialCurrentQueue, ^{
NSLog(@"串行队列-异步任务4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
执行完成
串行队列-异步任务1-<NSThread: 0x7f9f32e1de80>{number = 2, name = (null)}
串行队列-异步任务2-<NSThread: 0x7f9f32e1de80>{number = 2, name = (null)}
串行队列-异步任务3-<NSThread: 0x7f9f32e1de80>{number = 2, name = (null)}
串行队列-异步任务4-<NSThread: 0x7f9f32e1de80>{number = 2, name = (null)}
*/
- 异步任务 + 并行队列
开启新线程,同步执行任务。
dispatch_queue_t conCurrentQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(conCurrentQueue, ^{
NSLog(@"并发队列-异步任务1-%@", [NSThread currentThread]);
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"并发队列-异步任务2-%@", [NSThread currentThread]);
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"并发队列-异步任务3-%@", [NSThread currentThread]);
});
dispatch_async(conCurrentQueue, ^{
NSLog(@"并发队列-异步任务4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
执行完成
并发队列-异步任务1-<NSThread: 0x7fb751e1a6a0>{number = 5, name = (null)}
并发队列-异步任务3-<NSThread: 0x7fb751d08830>{number = 6, name = (null)}
并发队列-异步任务2-<NSThread: 0x7fb751d0ee50>{number = 2, name = (null)}
并发队列-异步任务4-<NSThread: 0x7fb751d0f340>{number = 4, name = (null)}
或
并发队列-异步任务2-<NSThread: 0x7fb751d01d20>{number = 10, name = (null)}
执行完成
并发队列-异步任务1-<NSThread: 0x7fb751d06210>{number = 9, name = (null)}
并发队列-异步任务3-<NSThread: 0x7fb751d0f400>{number = 11, name = (null)}
并发队列-异步任务4-<NSThread: 0x7fb751d08830>{number = 8, name = (null)}
*/
- 异步任务 + 主线程
不开启新线程,在主线程串行执行任务。
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSLog(@"主队列-异步任务1-%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"主队列-异步任务2-%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"主队列-异步任务3-%@", [NSThread currentThread]);
});
dispatch_async(mainQueue, ^{
NSLog(@"主队列-异步任务4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
执行完成
主队列-异步任务1-<NSThread: 0x7f8ccb601ec0>{number = 1, name = main}
主队列-异步任务2-<NSThread: 0x7f8ccb601ec0>{number = 1, name = main}
主队列-异步任务3-<NSThread: 0x7f8ccb601ec0>{number = 1, name = main}
主队列-异步任务4-<NSThread: 0x7f8ccb601ec0>{number = 1, name = main}
*/
- 同步任务 + 串行队列
不开启新线程,串行执行任务。
dispatch_queue_t serialCurrentQueue = dispatch_queue_create("myQueue", NULL);
dispatch_sync(serialCurrentQueue, ^{
NSLog(@"串行队列-同步任务1-%@", [NSThread currentThread]);
});
dispatch_sync(serialCurrentQueue, ^{
NSLog(@"串行队列-同步任务2-%@", [NSThread currentThread]);
});
dispatch_sync(serialCurrentQueue, ^{
NSLog(@"串行队列-同步任务3-%@", [NSThread currentThread]);
});
dispatch_sync(serialCurrentQueue, ^{
NSLog(@"串行队列-同步任务4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
串行队列-同步任务1-<NSThread: 0x7f99c8600190>{number = 1, name = main}
串行队列-同步任务2-<NSThread: 0x7f99c8600190>{number = 1, name = main}
串行队列-同步任务3-<NSThread: 0x7f99c8600190>{number = 1, name = main}
串行队列-同步任务4-<NSThread: 0x7f99c8600190>{number = 1, name = main}
执行完成
*/
- 同步任务 + 并行队列
不开启新线程,串行执行任务。
dispatch_queue_t conCurrentQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(conCurrentQueue, ^{
NSLog(@"并发队列-同步任务1-%@", [NSThread currentThread]);
});
dispatch_sync(conCurrentQueue, ^{
NSLog(@"并发队列-同步任务2-%@", [NSThread currentThread]);
});
dispatch_sync(conCurrentQueue, ^{
NSLog(@"并发队列-同步任务3-%@", [NSThread currentThread]);
});
dispatch_sync(conCurrentQueue, ^{
NSLog(@"并发队列-同步任务4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
并发队列-同步任务1-<NSThread: 0x7fc1d2d08820>{number = 1, name = main}
并发队列-同步任务2-<NSThread: 0x7fc1d2d08820>{number = 1, name = main}
并发队列-同步任务3-<NSThread: 0x7fc1d2d08820>{number = 1, name = main}
并发队列-同步任务4-<NSThread: 0x7fc1d2d08820>{number = 1, name = main}
执行完成
*/
- 同步任务 + 主队列
阻塞主线程
dispatch_queue_t mainQueue = dispatch_get_main_queue();
NSLog(@"任务开始");
dispatch_sync(mainQueue, ^{
NSLog(@"主队列-同步任务1-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*主队列等待同步任务完成,同步任务等待主队列执行完,相互等待,谁都完成不了。
任务开始
*/
表格总结
全局、并发队列 | 自建串行队列 | 主队列 | |
---|---|---|---|
同步任务 | 不开启新线程;串行执行任务 | 不开启新线程;串行执行任务 | 造成死锁 |
异步任务 | 开启新线程;并发执行任务 | 开启新线程;串行执行任务 | 不开启新线程;串行执行任务 |
GCD调度组(线程组)
代码示例异步执行多个任务,当所有任务执行完成后,再执行某个任务。
GCD线程间通讯
主队列=主线程=UI线程,所有的界面刷新都需要在主线程进行,子线程完成操作后,拿到主线程,在主线程进行刷新界面。
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
//执行耗时任务
//...
dispatch_async(dispatch_get_main_queue(), ^{
//在主线程进行界面刷新
//...
});
});
GCD延时函数
//第一种
[self performSelector:@selector(showMessage) withObject:nil afterDelay:2.0f];
//第二种
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//2秒后执行的操作
//...
});
GCD执行一次函数
提示方法static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只需要执行一次的代码
//...
});
GCD栅栏函数
在并发队列插入栅栏函数,会先执行栅栏函数之前的操作,再执行栅栏函数,最后执行栅栏函数后面的操作,起到栅栏的作用。
dispatch_queue_t queue = dispatch_queue_create("name", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"耗时操作1-%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"耗时操作2-%@", [NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"栅栏函数-%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"耗时操作3-%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"耗时操作4-%@", [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
执行完成
耗时操作2-<NSThread: 0x7ff4b2dad620>{number = 2, name = (null)}
耗时操作1-<NSThread: 0x7ff4b2c04bc0>{number = 3, name = (null)}
栅栏函数-<NSThread: 0x7ff4b2c04bc0>{number = 3, name = (null)}
耗时操作4-<NSThread: 0x7ff4b2dad620>{number = 2, name = (null)}
耗时操作3-<NSThread: 0x7ff4b2c04bc0>{number = 3, name = (null)}
*/
GCD-dispatch_apply()函数
提交任务到队列中多次执行,由队列决定并行还是串行。dispatch_apply为同步任务,执行完dispatch_apply才会执行下面的操作。
/* iterations 执行的次数
queue 提交到的队列
size_t 索引,需要自己指定一个参数
block 执行的任务
dispatch_apply(size_t iterations, dispatch_queue_t queue, <#^(size_t)block#>)
*/
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
dispatch_apply(4, globalQueue, ^(size_t index) {
//执行的循环操作
NSLog(@"次数%d-%@", index, [NSThread currentThread]);
});
NSLog(@"执行完成");
/*
次数0-<NSThread: 0x7f9da35020d0>{number = 1, name = main}
次数1-<NSThread: 0x7f9da340be60>{number = 2, name = (null)}
次数3-<NSThread: 0x7f9da3740740>{number = 4, name = (null)}
次数2-<NSThread: 0x7f9da3608440>{number = 3, name = (null)}
执行完成
*/
网友评论