Swift中DispatchQueue类型之间的差异 [英] Difference between DispatchQueue types in swift

查看:1177
本文介绍了Swift中DispatchQueue类型之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,迅速有3种DispatchQueue:

As I understand there are 3 types of DispatchQueue in swift:

  • 主(串行)(主线程)
  • 全局(并行)(背景线程并行工作)
  • 自定义(并行或串行)
  • Main (serial) (Main Thread)
  • Global (Concurrent) (Background Threads working in parallel)
  • Custom (Concurrent or serial)

每个人都可以工作(异步或同步)

And each one maybe work (asynch or synch)

第一个问题:

主队列是否仅在 UI线程上工作,而不在其他线程上工作? 如果答案是肯定的,那么DispatchQueue.Main.async如何不阻止UI线程. 如果答案为否,只要DispatchQueue.Main.async在另一个线程中工作,使用DispatchQueue.global有什么好处.

Is it main queue working on UI thread only and not working on another thread? If the answer yes , how DispatchQueue.Main.async not blocking UI thread. If the answer No , what is the benefit of using DispatchQueue.global as long as DispatchQueue.Main.async work in another thread.

第二个问题:

DispatchQueue.global(异步) DispatchQueue.global(同步)之间的区别是什么,只要该队列工作并发,并且在哪里使用每个?

what is deference between DispatchQueue.global (async) and DispatchQueue.global (sync) as long as this queue working Concurrent and where to use each one?

第三个问题:

  1. (串行和同步)
  2. (并发和异步)

推荐答案

据我了解:

队列不是线程

主队列和全局队列可能在同一线程中工作

已分派:表示将任务放入队列

Dispatched: means put task in queue

如果全局队列作为 sync 主队列中调度,则调度的任务将在 Main队列的同一线程上工作和已调度的任务已添加到全局队列中, 此任务将冻结线程

If Global queue dispatched in Main queue as sync , the dispatched task will work on same thread of Main queue and dispatched task added to Global queue , And this task will freezing the thread

如果全局队列主队列中以 async 的身份分派,则分派的任务将在主队列的其他线程上起作用和已调度的任务已添加到全局队列中, 该任务不会冻结线程

If Global queue dispatched in Main queue as async , the dispatched task will work on other thread of Main queue and dispatched task added to Global queue , And this task will not freezing the thread

如果主队列主队列中以 async 的身份分派,则分派的任务将在主队列的同一线程上工作

If Main queue dispatched in Main queue as async , the dispatched task will work on same thread of Main queue

如果在主队列中作为同步调度的主队列将成为异常,因为进行死锁

If Main queue dispatched in Main queue as sync will make exception because make deadlock

Dispatch.sync :将任务放入队列中,直到完成为止

Dispatch.sync: put task in queue and wait it until finish

Dispatch.async :将任务放入队列中,而不要等到完成(该任务可以在同一线程或另一个线程中工作)

Dispatch.async: put task in queue and not wait it until finish (The task may work in same thread or in another thread)

  • 如果任务在全局队列上分派,并且这与主线程一致 然后该任务将添加到Global queue,新线程将是 创建,任务将立即在新线程中开始工作

  • If task dispatched on Global queue and this accord from Main thread then the task will added to Global queue , and new thread will be create and the task will start working immediately in the new thread

如果任务在Main队列上分派并且与Main线程一致 那么该任务将添加到主队列"中,并且将无法正常工作 立即直到队列中较旧的任务完成工作(因为Main 队列是顺序的)

If task dispatched on Main queue and this accord from Main thread then the task will added to Main queue , and will not work immediately until older tasks in queue finish working (because Main queue is sequential )

这篇关于Swift中DispatchQueue类型之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆