简体   繁体   中英

Swift Execution Queue

For example. I have Three background task. Currently running three threads and in threads is a code

Dispatchqueue.main.async { 
  //SOMETHING CODE
}

Can code in three different threads be executed simultaneously where there is Dispatchqueue.main.async ? Thank you!

The main queue is serial, so only one thing at a time will run on it. If multiple blocks are submitted to the main queue at the same time, they'll run sequentially.

As a rule, you should avoid thinking about this in terms of threads. Threads are generally an implementation detail that queues live on top of. (If you're actively creating your own threads, using pthreads for example, you should generally avoid that, and use queues.) Threads and queues are not one-to-one, except for the main thread and main queue, which are tied to each other.

This matters because you generally do not "run code on a thread." You dispatch a block to a queue, and GCD will schedule that block onto some thread. When you see it that way, then how the main queue behaves should become much clearer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM