There are three types of queue
Main:Tasks execute serially on your application's main thread
Concurrent:Tasks start executing in FIFO order, but can run concurrently.
Serial:Tasks execute one at a time in FIFO order
- (1) Serial queues (Serial Queue), also known as private scheduling queue, is generally used for Synchronous access to specific resources. We can create any number of serial queues as needed. Each serial queue is concurrent.
- (2) Parallel queue, also known as global dispatch queue. Although parallel queues can run multiple tasks concurrently, the execution sequence of the tasks starts is the same as that of the tasks added to the queue. We cannot create parallel scheduling queues by ourselves. There are only three available global concurrent queues.
- (3) Main dispatch queue is a globally available serial queue.Program. Tasks in this queue alternate with the event source to be executed in the main loop of the application. Because it runs in the main thread of the application, main queue is often used as a synchronization point of the application.
Important functions in this c API
Creating and releasing queues
Dispatch_queue_t dispatch_queue_create (const char * label, null );// Serial queue
Void dispatch_release (dispatch_queue_t );
Putting blocks in the queue
TypedefVoid(^ Dispatch_block_t) (void );
VoidDispatch_async (dispatch_queue_t queue, dispatch_block_t block );
Getting the current or main queue
Dispatch_queue_tDispatch_get_current_queue ();
VoidDispatch_queue_retain (dispatch_queue_t );// Keep it in the heapDispatch_release
Main queue:Dispatch_queue_tDispatch_get_main_queue ();
Global queue: dispatch_get_global_queue (dispatch_queue_priority_default, 0 );
Serial: dispatch_queue_create ("identifier", null );
Example... assume we fetched an image from the Network (this wocould be slow ).
-(Void) viewwillappear :( bool) animated
{Dispatch_queue_t downloadqueue = dispatch_queue_create ("image downloader", null );
Dispatch_async (downloadqueue, ^ {nsdata * imagedata = [nsdata datawithcontentsofurl: networkurl];
// Uikit can only be dispatch_async (dispatch_get_main_queue (), ^ {
Uiimage * image = [uiimage imagewithdata: imagedata];
Self. imageview. Image = image;
Self. imageview. Frame = cgrectmake (0, 0, image. Size. Width, image. Size. Height );
Self. scrollview. contentsize = image. size ;});
}); Dispatch_release (downloadqueue );
}