iOS 关于队列线程和后台异步运行程序

来源:互联网 发布:淘宝卖的红酒能喝吗 编辑:程序博客网 时间:2024/05/19 23:16

  • 关于不同队列的比较:

Serial queues are monogamous, but uncommitted. If you give a bunch of tasks to each serial queue, it will run them one at a time, using only one thread at a time. The uncommitted aspect is that serial queues may switch to a different thread between tasks. Serial queues always wait for a task to finish before going to the next one. Thus tasks are completed in FIFO order. You can make as many serial queues as you need with dispatch_queue_create.

The main queue is a special serial queue. Unlike other serial queues, which are uncommitted, in that they are "dating" many threads but only one at time, the main queue is "married" to the main thread and all tasks are performed on it. Jobs on the main queue need to behave nicely with the runloop so that small operations don't block the UI and other important bits. Like all serial queues, tasks are completed in FIFO order.

If serial queues are monogamous, then concurrent queues are promiscuous. They will submit tasks to any available thread or even make new threads depending on system load. They may perform multiple tasks simultaneously on different threads. It's important that tasks submitted to the global queue are thread-safe and minimize side-effects. Tasks are submitted for execution in FIFO order, but order of completion is not guaranteed. As of this writing, there are only three concurrent queues and you can't make them, you can only fetch them with dispatch_get_global_queue.

  • 代码实例:

后台异步运行程序 1-Serial queue:

dispatch_queue_t exampleQueue = dispatch_queue_create( "xmpp_message", NULL );dispatch_async(exampleQueue, ^{    NSLog(@"%s dispatched to xmpp_message", __FUNCTION__);    // code for processing messages....    dispatch_queue_t queue = dispatch_get_main_queue();    dispatch_async(queue, ^{        NSLog(@"%s     re-dispatched to main queue", __FUNCTION__);        [self sendNotification:msg];        NSLog(@"%s     finished dispatch to main queue", __FUNCTION__);    });    NSLog(@"%s finished dispatched to xmpp_message", __FUNCTION__);});// if not ARC or supporting iOS versions prior to 6.0, you should release the queuedispatch_release(exampleQueue);

后台异步运行程序 2-Concurrent queue:

dispatch_queue_t exampleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // or in  recent versions of iOS, you can use dispatch_queue_create( "xmpp_message", DISPATCH_QUEUE_CONCURRENT );dispatch_async(exampleQueue, ^{    NSLog(@"%s dispatched to xmpp_message", __FUNCTION__);    // code for processing messages....    dispatch_queue_t queue = dispatch_get_main_queue();    dispatch_async(queue, ^{        NSLog(@"%s re-dispatched to main queue", __FUNCTION__);        [self sendNotification:msg];    });});

  • 官方说明

Table 3-1  Types of dispatch queues

Type

Description

Serial

Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue. The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue. Serial queues are often used to synchronize access to a specific resource.

You can create as many serial queues as you need, and each queue operates concurrently with respect to all other queues. In other words, if you create four serial queues, each queue executes only one task at a time but up to four tasks could still execute concurrently, one from each queue. For information on how to create serial queues, see “Creating Serial Dispatch Queues.”

Concurrent

Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue. The exact number of tasks executing at any given point is variable and depends on system conditions.

In iOS 5 and later, you can create concurrent dispatch queues yourself by specifying DISPATCH_QUEUE_CONCURRENT as the queue type. In addition, there are four predefined global concurrent queues for your application to use. For more information on how to get the global concurrent queues, see “Getting the Global Concurrent Dispatch Queues.”

Main dispatch queue

The main dispatch queue is a globally available serial queue that executes tasks on the application’s main thread. This 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. Because it runs on your application’s main thread, the main queue is often used as a key synchronization point for an application.

Although you do not need to create the main dispatch queue, you do need to make sure your application drains it appropriately. For more information on how this queue is managed, see “Performing Tasks on the Main Thread.”

参考链接:

http://stackoverflow.com/a/13899386/3458781

http://stackoverflow.com/questions/13624734/how-to-process-dispatch-async-process-in-background

https://developer.apple.com/library/mac/documentation/general/conceptual/concurrencyprogrammingguide/OperationQueues/OperationQueues.html

0 0