《Concurrency Programming Guide》 笔记


1 A dispatchqueue executes taskseither serially or concurrently but always in a first-in, first-out order.

dispatch queue总是以FIFO的方式,串行的或并发的执行任务。

serial dispatch queue每次只能执行一个任务,下一个任务只能等待它执行完成,才可以运行。

concurrent dispatch queue每次尽可能执行最多的任务,而不需要等待已经执行的任务执行完成。

 

2 Blocks can also be moved out of their original scope andcopied onto the heap, which is what happens when you submit them to a dispatchqueue.

Block能过从它们原来的作用域移出,拷贝到堆上。这也是将Block提交到dispatch queue时发生的事情。

 

3 Dispatch sources are a C-based mechanism for processingspecific types of system events asynchronously. A dispatch source encapsulatesinformation about a particular type of system event and submits a specific block object or function to a dispatch queue whenever that eventoccurs. 

Dispatch Sources是为了异步处理特定类型的系统事件的基于C的机制。Dispatchsource封装了特定类型的系统事件,当事件发生时,将特定的block object或者函数提交到dispatch queue

 

 

4 Operation queues: NSOperation类的一个实例。NSOperation是一个抽象类,必须使用派生的子类去执行任务。我们可以使用Foundation框架提供的两个具体的NSOperation子类,分别是NSInvocationOperationNSBlockOperation

 

5 Grand Central Dispatch (GCD) dispatch queues is anobject-like structure. All dispatch queues are FIFO.

 

Dispatchqueue Types:

  • Serial: Serial queues  (private dispatch queues)每次执行一个任务,按照任务添加的顺序执行。执行的任务运行在一个独特的线程,该线程由dispatch queue管理。Serial queues通常用于同步地访问一些特殊的资源。

 

  • Concurrent: Concurrent queues (global dispatch queue) 按照任务添加顺序,并发的执行任务。在给定的时间点上,执行的任务数目也是不定的,取决于系统条件。系统中有4个预定义的global concurrent queues提供给应用使用。

 

  • Main dispatch queue: Main dispatch queue  is a globally available serial queue that executes tasks on the application’s main threadThis queue works with the application’s run loop (if one is present) to interleave the execution of queued tasks with the execution of other event sources attached to the run loop.

 

6 The key to writing your code is to design tasks that areself-contained and able to run asynchronously. (This is actually true for boththreads and dispatch queues.) 

 

7 获取global  concurrent DispatchQueues:

dispatch_queue_t aQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

 

DISPATCH_QUEUE_PRIORITY_DEFAULT default优先级的队列。

0 - 保留位。

 

8 创建 serial queue :

Code:

dispatch_queue_t queue;

queue =dispatch_queue_create("com.example.MyQueue", NULL);

 

Explain:

The dispatch_queue_create functiontakes two parameters: the queue name and a set of queue attributes.

The queue attributes are reserved for future use and should be NULL.

 

9 Grand Central Dispatch provides functions to let you accessseveral common dispatch queues from your application:

 

 

  • Use the dispatch_get_main_queue function to get the serial dispatch queue associated with your application’s main thread. 

 

 

10 Adding a Single Task to aQueue:

 

There are two ways to add a task to a queue: asynchronously orsynchronously. When possible, asynchronous execution using the dispatch_async and dispatch_async_f functions ispreferred over the synchronous alternative. 

 

Although you should add tasks asynchronously whenever possible,there may still be times when you need to add a task synchronously to preventrace conditions or other synchronization errors. In these instances, you canuse the dispatch_sync and dispatch_sync_f functions to addthe task to the queue. 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值