Difference between DispatchQueue types in swift

后端 未结 2 1739

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

  • Main (serial) (Main Thread)
  • Global (Concurrent) (Background
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-11 01:35

    As I understand:

    Queue is not Thread

    Main and global queue may work in same thread

    Dispatched: means put task in queue

    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

    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

    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: put task in queue and wait it until finish

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

    • 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

    • 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 )

提交回复
热议问题