Submits a block for asynchronous execution on a dispatch queue and returns immediately.
提交一个块以在调度队列上异步执行并立即返回。
code showing
- 以一个最简单的demo开始
// 创建一个同步队列
dispatch_queue_t syncQueue = dispatch_queue_create("io.sqi.MySyncQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"0️⃣ zero, current thread: %@", [NSThread currentThread]);
// 使用 dispatch_async 在 syncQueue 上异步执行
dispatch_async(syncQueue, ^{
NSLog(@"2️⃣ 2nd, current thread: %@", [NSThread currentThread]);
});
NSLog(@"1️⃣ 1st, current thread: %@", [NSThread currentThread]);
在主线程上执行以上代码, 运行结果如下:
0️⃣ zero, <_NSMainThread: 0x60000170c000>{number = 1, name = main}
1️⃣ 1st, <_NSMainThread: 0x60000170c000>{number = 1, name = main}
2️⃣ 2nd, <NSThread: 0x6000017427c0>{number = 7, name = (null)}
可以看到,为了异步执行 block, 系统开辟了一条 number = 7 的新线程
- 加入同步执行任务
// 创建一个同步队列
dispatch_queue_t syncQueue = dispatch_queue_create("io.sqi.MySyncQueue", DISPATCH_QUEUE_SERIAL);
NSLog(@"0️⃣ zero, current thread: %@", [NSThread currentThread]);
// 使用 dispatch_async 在 syncQueue 上异步执行
dispatch_async(syncQueue, ^{
NSLog(@"1️⃣ 1st, current thread: %@", [NSThread currentThread]);
});
// 使用 dispatch_sync 在 syncQueue 上同步执行
dispatch_sync(syncQueue, ^{
NSLog(@"2️⃣ 2nd, current thread: %@", [NSThread currentThread]);
});
NSLog(@"3️⃣ 3rd, current thread: %@", [NSThread currentThread]);
在主线程上执行以上代码, 运行结果如下:
0️⃣ zero, <_NSMainThread: 0x6000017080c0>{number = 1, name = main}
1️⃣ 1st, <NSThread: 0x60000171c940>{number = 5, name = (null)}
2️⃣ 2nd, <_NSMainThread: 0x6000017080c0>{number = 1, name = main}
3️⃣ 3rd, <_NSMainThread: 0x6000017080c0>{number = 1, name = main}
因为2nd
需要同步执行,所以 2nd 的执行要在 3rd 前面,因为1st
是先加入 syncQueue, 所以 1st 的执行要在 2nd 前面
- 变更为并发队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("io.sqi.MyConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"0️⃣ zero, current thread: %@", [NSThread currentThread]);
// 使用 dispatch_async 在 concurrentQueue 上异步执行
dispatch_async(concurrentQueue, ^{
NSLog(@"1️⃣ 1st, current thread: %@", [NSThread currentThread]);
});
// 使用 dispatch_sync 在 concurrentQueue 上同步执行
dispatch_sync(concurrentQueue, ^{
NSLog(@"2️⃣ 2nd, current thread: %@", [NSThread currentThread]);
});
NSLog(@"3️⃣ 3rd, current thread: %@", [NSThread currentThread]);
同样在 main thread 上执行,运行结果如下:
// 情况一
0️⃣ zero, <_NSMainThread: 0x600001708000>{number = 1, name = main}
2️⃣ 2nd, <_NSMainThread: 0x600001708000>{number = 1, name = main}
1️⃣ 1st, <NSThread: 0x6000017537c0>{number = 5, name = (null)}
3️⃣ 3rd, <_NSMainThread: 0x600001708000>{number = 1, name = main}
// 情况二
0️⃣ zero, <_NSMainThread: 0x600001708000>{number = 1, name = main}
2️⃣ 2nd, <_NSMainThread: 0x600001708000>{number = 1, name = main}
3️⃣ 3rd, <_NSMainThread: 0x600001708000>{number = 1, name = main}
1️⃣ 1st, <NSThread: 0x6000017537c0>{number = 5, name = (null)}
- 因为是并发队列,1st 无需等待 2nd 执行完毕才开始,可以同时执行;
- dispatch_sync 不具备开启新线程的能力,所以依旧需要在 main thread 上执行;
- 为什么 2nd 总是在 3rd 的前面 ?
分析如下:首先 dispatch_async 将任务放入队列中后立即返回,不阻塞当前线程,所以 2nd 总是会立即执行,而 dispatch_async 提交的队列的 block 需要开辟新的线程以执行 block 中的任务,这是一个耗时操作;