名詞解釋

Thread:A thread of execution can be thought of as a CPU executing the program.

  1. Single Thread:…….
  2. Multi-Thread:When you have multiple threads executing the same program, it is like having multiple CPUs executing within the same program.

Task/Request:Queue內各Task/Request執行和返回的方式

OperationQueue=>Operation subclass/ Block

GCD=>Block

  1. Synchronous Execution:Wait for one task to finish before moving on to another task. A synchronous function returns only after the completion of a task that it orders.
  2. Asynchronous Execution:Execute something asynchronously, you can move on to another task before it finishes. An asynchronous function, returns immediately, ordering the task to be done but does not wait for it. Thus, an asynchronous function does not block the current thread of execution from proceeding on to the next function.

Operation Queue(Dispatch Queue):A data structure, execute operations by adding them to an operation queue

GCD : 建立了Queue,就會到新的Thread中執行

GCD:可選Serial或Concurrent,OperationQueue:預設就是Concurrent

GCD:FIFO,OperationQueue:None-FIFO,by priority

Queue在哪個Thread上執行,會由系統分配,由pool of threads(執行緒池)中挑選出來。

  1. Serial Queues:Tasks in serial queues execute one at a time, each task starting only after the preceding task has finished.
  2. Concurrent Queues:Tasks in concurrent queues are guaranteed to start in the order they were added and that’s it and you don’t have any knowledge when they are gonna finish. Process multiple requests/tasks at a time inside a thread.

差異:

Serial / Concurrent:Queue內每個Task的執行方式。一次一個Task或一次多個Task。

Synchronous / Asynchronous :每個Task內的執行和返回的方式。Synchronous若在Main Thread上,則會卡住畫面。


Thread Safe

Thread safe code can be safely called from multiple threads or concurrent tasks without causing any problems.