Taking notes... -------
1. Serial queue-One after another execution
Create a queue
dispatch_queue_t q = dispatch_queue_create ("GCD1", dispatch_queue_serial);
for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
for (int i = 0; i < ++i) {
asynchronous tasks, which are executed concurrently, but if in the serial queue, they are still sequentially executed
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
2. Parallel queue
Features: No formation, execution sequence programmer can not control!
Scenario: Concurrent execution tasks, no sequencing relationship
Parallel queues are prone to errors! Parallel queues do not control the number of new threads!
dispatch_queue_t q = dispatch_queue_create ("Gcd2", dispatch_queue_concurrent);
for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
for (int i = 0; i < ++i) {
Asynchronous tasks are executed in different threads
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
3. Global Queue (Apple provides a global queue for easy multi-threaded design for all apps to work with)
The difference between a global queue and a parallel queue
1> does not need to be created, direct get can be used
2> two queues perform the same effect
3> Global queue does not have a name, the exact queue cannot be confirmed when debugging
dispatch_queue_t Q =dispatch_get_global_queue (dispatch_queue_priority_default, 0);
for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
for (int i = 0; i < ++i) {
asynchronous tasks, which are executed concurrently, but are still executed sequentially if in the walkthrough queue
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}
4. Primary (thread) queue, which ensures that operations are performed on the main thread
Each application has only one main thread
Why do I need to work on the main thread?
In iOS development, all UI updates must be done on the main thread!
dispatch_queue_t q = Dispatch_get_main_queue ();
The main thread is working, and unless the program is killed, the main thread's work will never end!
Blocked!!! So don't use sync
Dispatch_sync (q, ^{
NSLog (@ "Come here baby!");
});
Asynchronous tasks that run on the main thread while maintaining formation
for (int i = 0; i < ++i) {
Dispatch_async (q, ^{
NSLog (@ "%@-%d", [Nsthread CurrentThread], i);
});
}
GCD Base (Queue)