GCD demo example

Source: Internet
Author: User

1. GCD. h

# Import <uikit/uikit. h>

@ Interface viewcontroller: uiviewcontroller

@ Property (retain, nonatomic) iboutlet uilabel * outletlabel1;
@ Property (retain, nonatomic) iboutlet uilabel * outletlabel2;
@ Property (retain, nonatomic) iboutlet uilabel * outletlabel3;
@ Property (retain, nonatomic) iboutlet uilabel * outletlabel4;

-(Ibaction) btnclick4 :( uibutton *) sender;
-(Ibaction) btnclick2 :( uibutton *) sender;
-(Ibaction) btnclick3 :( uibutton *) sender;
-(Ibaction) btnclick :( uibutton *) sender;

@ End

2. GCD. m

/*
Introduction to gcd (System Management thread)
 
To use GCD, you don't need to write thread code. You just need to define the task to be executed and add it to the appropriate dispatch Queue (scheduling Queue ).
GCD is responsible for creating threads and scheduling your tasks. The system directly provides thread management, which is more efficient than application implementation.
 
Based on the custom task execution mechanism of C, the dispatch queue executes the task serially or concurrently in the FIFO order. The dispatch queue is divided into the following three types:
 
Serial dispatch queue: A serial scheduling queue that executes only one task at a time and starts the next task only after the current task is completed.
It is mainly used for Synchronous access to specific resources. Although each serial queue can only execute one task at a time, each serial queue is concurrently executed.
 
Concurrent Dispatch queue: Also known as global dispatch queue. It schedules queues in parallel and executes one or more tasks concurrently, but the startup sequence is still added
You cannot create Concurrent Dispatch queues for the sequential start of queue. You can only use the three global concurrent queues defined by the system,
The details are as follows:
Main dispatch queue: globally available serial queue, which executes tasks in the main application thread, for example, used to refresh the UI

 
Technologies related to dispatch queue
Dispatch group: used to monitor the completion of a group of block objects
Dispatch semaphore: similar to the traditional semaphore (semaphore)
Dispatch Source: asynchronous processing of System Events
 
Each thread in the dispatch queue can share data through the context pointer of the queue.
 
*/

# Import "viewcontroller. H"

@ Interface viewcontroller ()

@ End

@ Implementation viewcontroller

-(Void) viewdidload
{
[Super viewdidload];
// Do any additional setup after loading the view, typically from a nib.
}

-(Void) didreceivememorywarning
{
[Super didreceivememorywarning];
// Dispose of any resources that can be recreated.
}

/*
Global Concurrent Dispatch queue
1. Concurrent Dispatch queue can execute multiple tasks concurrently, but the concurrent queue still starts the task in the FIFO order.
2. the number of concurrent tasks executed by the concurrent queue varies dynamically according to the application and system. Various factors include the number of available cores, the number of jobs being executed by other processes, and the number of priority tasks in other serial dispatch queue.
3. The system will provide three Concurrent Dispatch queue for each application for global sharing. The only difference between the three queue is that the priority is different.
*/
-(Ibaction) btnclick4 :( uibutton *) sender {
 
/*
Obtain global concurrency dispatch queue: dispatch_get_global_queue (dispatch_queue_priority_default, 0)
The first parameter indicates the priority of the queue. Here, you can obtain the queue with the default priority. You can also get the queue with the high or low priority and change the default value to high or low.
The second parameter represents?
*/
Dispatch_queue_t aqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );


// Add a block to the global concurrent scheduling queue
Dispatch_async (aqueue, ^ (void ){

For (INT I = 0; I <1000; I ++ ){

// Add the block to the main thread synchronously. In this way, the demo queue will be paused until the execution of the block is complete.
Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Nsstring * text = [nsstring stringwithformat: @ "% d", I];
Self. outletlabel3.text = text;
});
}

});
 

// Add another block to the global concurrent scheduling queue. This block and the block above will be executed concurrently
Dispatch_async (aqueue, ^ (void ){

For (INT I = 0; I <1000; I ++ ){

// Add the block to the main thread synchronously. In this way, the demo queue will be paused until the execution of the block is complete.
Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Nsstring * text = [nsstring stringwithformat: @ "% d", I];
Self. outletlabel4.text = text;
});
}

});

}

/*
Serial dispatch queue
1. the serial queue can only execute one task at a time. It can be used to replace the lock to protect shared resources or variable data structures, serial queue ensures that tasks are executed in predictable order (this is better than locking)
2. You must explicitly create and manage your serial Queue (any number)
*/
-(Ibaction) btnclick2 :( uibutton *) sender {

/*
Use the dispatch_queue_create () method to create a serial queue
The first parameter indicates the queue name, and the second parameter indicates a set of attributes of the queue (reserved for future use)
*/
Dispatch_queue_t queue = dispatch_queue_create ("demo queue", null );


/*
Asynchronous scheduling and synchronous scheduling
Asynchronous scheduling dispatch_async: After a task is added to a queue, it leaves immediately regardless of the execution status of the task in that queue.
Synchronous scheduling dispatch_sync: After a task is added to a queue, the calling thread will continue to execute the task after the task is completed. Nima, potholes

Therefore, the difference between asynchronous scheduling and synchronous scheduling is not how the added task is executed, but whether the call thread waits for the task to be executed.
*/

// Asynchronously Add the block to the scheduling queue named demo queue created above
Dispatch_async (queue, ^ (void ){

For (INT I = 0; I <1000; I ++ ){

// Sleep (1 );
// Printf ("A % d \ t", I );

// Add the block to the main thread synchronously. In this way, the demo queue will be paused until the execution of the block is complete.
Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Nsstring * text = [nsstring stringwithformat: @ "% d", I];
Self. outletlabel1.text = text;
// Printf ("B % d \ n", I );
});

}

});

// Because demo queue is a serial scheduling queue, the following block will start after the above block is executed
Dispatch_async (queue, ^ (void ){

For (INT I = 0; I <1000; I ++ ){

Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Self. outletlabel2.text = [nsstring stringwithformat: @ "% d", I];

});
}


});


// The object of xxxxx_create must correspond to xxxxx_release?
Dispatch_release (Queue );

// The screen will be stuck in this loop ~~~
// For (INT I = 0; I <100000; I ++ ){
// Self. outletlabel1.text = [nsstring stringwithformat: @ "% d", I];
// Printf ("% d \ n", I );
//}
}

/*
Dispatch_group can put a group of tasks in a group and continue to run after all tasks in the group are executed.
*/
-(Ibaction) btnclick3 :( uibutton *) sender {

// Reset the text of label2
Self. outletlabel2.text = @ "begin ";

// Obtain a global concurrent scheduling queue
Dispatch_queue_t queue = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

// Create a dispatch Group
Dispatch_group_t group = dispatch_group_create ();

// Define Task 1
Dispatch_block_t task1 = ^ (void ){

For (INT I = 0; I <300; I ++ ){

Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Self. outletlabel3.text = [nsstring stringwithformat: @ "% d", I];
});
}

};

// Define Task 2
Dispatch_block_t task2 = ^ (void ){

For (INT I = 0; I <300; I ++ ){

Dispatch_sync (dispatch_get_main_queue (), ^ (void ){

Self. outletlabel4.text = [nsstring stringwithformat: @ "% d", I];
});
}

};

// Associate task1 with queue and group
Dispatch_group_async (group, queue, task1 );

// Associate task2 with queue and group
Dispatch_group_async (group, queue, task2 );


// After all tasks in the group are executed, execute the notify method to merge the wait method and the code to be executed later.
Dispatch_group_policy (group, dispatch_get_main_queue (), ^ (void ){

Self. outletlabel2.text = @ "done! ";

});

// Xxx_create to create the corresponding xxx_release ()
Dispatch_release (group );
}

/*
Dispatch Source
*/
-(Ibaction) btnclick :( uibutton *) sender {

}

-(Void) dealloc {

[_ Outletlabel1 release];
[_ Outletlabel2 release];
[_ Outletlabel3 release];
[_ Outletlabel4 release];
[Super dealloc];
}

@ End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.