GCD concept: Grand Central Dispatch
1.queue Queue dispatch Queue distribution queues
2.task Tasks
3.thread Threads
Task is a separate assignment (method, function, block)
One or more tasks are stored in the queue.
Thread to ensure that the task can execute smoothly, the queue will open up the appropriate thread to execute the task within the thread that is opened.
There are two types of queue:
1.serialqueue (serial queue)
Features: The task is first out. The previous task executes first, and subsequent tasks are executed before execution begins.
2.concurrentQueue (parallel queue)
Features: Task first-out, in front of the task first execution, once started, the subsequent task will begin to execute (no need to wait for the end of a task)
The task follows the rules of the queue in which the task is to be placed.
Serialqueue
-(Ibaction) Click1: (UIButton *) Sender {
If you want to make the task serial, use the Serialqueue
METHOD1: System Serialqueue (mainqueue single case)
Mainqueue will only open a thread for the task (no matter how many tasks), and this thread is the main path
//?? These tasks are executed in the main thread, and will not be stuck on the main thread? Why does the system also provide mainqueue?
The biggest role of the main thread is to refresh the UI and do communication between threads.
dispatch_queue_t queue = Dispatch_get_main_queue ();
Add a task
Dispatch_async (Queue, ^{
NSLog (@ "Task 1 is the main thread:%d,%@", [Nsthread ismainthread],[nsthread CurrentThread]); 1
// });
METHOD2: Create a serialqueue//solve serial problem and sync problem
The serialqueue created by yourself also creates a thread for the task (no matter how many), which is a child thread that neither snaps to the main thread, but also implements thread synchronization
dispatch_queue_t queue = Dispatch_queue_create ("Createa. Serialqueue ", dispatch_queue_serial);
Dispatch_async (Queue, ^{
NSLog (@ "Task 1 is the main thread:%d,%@", [Nsthread ismainthread],[nsthread CurrentThread]); 0
});
All objects that use create must be freed
Dispatch_release (queue);}
Concurrentqueue
-(Ibaction) Click2: (UIButton *) Sender {
If you want to perform tasks concurrently, use the Concurrentqueue
METHOD1: The system's Globalqueue (singleton) is enough to use the system.
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Dispatch_async (Queue, ^{
NSLog (@ "Task 1 is the main thread:%d,%@", [Nsthread ismainthread],[nsthread CurrentThread]); 0
//
If you need to refresh the interface Dispatch_get_main_queue is generally used to refresh the interface
dispatch_queue_t mainqueue = Dispatch_get_main_queue ();
Dispatch_async (Mainqueue, ^{
NSLog (@ "Refresh UI");
// });
// });
METHOD2: Create a Concurrentqueue not used
dispatch_queue_t queue = dispatch_queue_create ("adddd", dispatch_queue_concurrent);
Dispatch_async (Queue, ^{
NSLog (@ "Task 1 is the main thread:%d,%@", [Nsthread ismainthread],[nsthread CurrentThread]); 0
});
}
//
-(Ibaction) Click3: (UIButton *) Sender {
//
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Dispatch_time Poptiem = Dispatch_time (Dispatch_time_now, (int64_t) 3 * nsec_per_sec);
Delay execution
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) 3 * nsec_per_sec), Dispatch_get_main_queue (), ^ (void) {
NSLog (@ "delay");
});
}
-(Ibaction) Click4: (UIButton *) Sender {
Nsarray *arr = @[@ "Red mansions", @ "Outlaws of the Marsh", @ "Journey to the Monkey", @ "kingdoms"];
Using concurrent to get the queue for concurrency, apply in concurrent execution of the array length of the task, after the start of the first task, the subsequent task also began to execute, the results of the output is unordered
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
dispatch_queue_t queue = dispatch_queue_create ("Apply", dispatch_queue_concurrent);
Repeat n times ^ (size_t) index random table below
Dispatch_apply (4, queue, ^ (size_t index) {
NSLog (@ "dispatch_apply");
NSLog (@ "%@", Arr[index]);//unordered
NSLog (@ "Asdfas");
});
}
Group
-(Ibaction) CLICK5: (UIButton *) Sender {
//
dispatch_queue_t queue = dispatch_queue_create ("group", dispatch_queue_concurrent);
dispatch_group_t group = Dispatch_group_create ();
Concurrent execution
Dispatch_group_async (group, queue, ^{
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) 3 * nsec_per_sec), Dispatch_get_main_queue (), ^ (void) {
NSLog (@ "group1");
});
});
Dispatch_group_async (group, queue, ^{
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) 3 * nsec_per_sec), Dispatch_get_main_queue (), ^ (void) {
NSLog (@ "GROUP5"); });
});
The method called in the group does not perform the Notify task, either in that queue, thread, or other location, until all of them have finished executing.
Dispatch_group_notify (group, queue, ^{
NSLog (@ "already go!"); /The group will not execute the method until all its tasks have been executed
});
}
When the read and write operations are in progress. Data may be different
-(Ibaction) Click6: (UIButton *) Sender {
//
dispatch_queue_t queue = dispatch_queue_create ("barrier", dispatch_queue_concurrent);
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
dispatch_queue_t queue = dispatch_queue_create ("barrier", dispatch_queue_concurrent);
Dispatch_async (Queue, ^{
NSLog (@ "20age");
});
Dispatch_async (Queue, ^{
NSLog (@ "10000");
});
The task after barrier is always executed after barrier execution (concurrency)
You can also use barrier for synchronous execution
Dispatch_barrier_async (Queue, ^{
NSLog (@ "Xieru");
});
Dispatch_async (Queue, ^{
NSLog (@ "Henan");
});
Dispatch_async (Queue, ^{
NSLog (@ "nan:");
});
Dispatch_release (queue);
}
-(Ibaction) Click7: (UIButton *) Sender {
//
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Can write a single case
NSLog (@ "Once");//can only be executed once
s = [[Singlton alloc]init];
return s;
});
}
-(Ibaction) CLICK8: (UIButton *) Sender {
dispatch_queue_t queue = dispatch_queue_create ("syn", dispatch_queue_concurrent);
After the block executes, the following code is executed (serial)
Dispatch_sync (Queue, ^{
for (int i = 0; i<10; i++) {
NSLog (@ "%d", I);
}
});
NSLog (@ "OK");
Dispatch_release (queue);
}
-(Ibaction) Click9: (UIButton *) Sender {
dispatch_queue_t queue = dispatch_queue_create ("Async_f", dispatch_queue_concurrent);
The context can be any type of argument
Dispatch_async_f (queue,@ "context", fucntion);
Dispatch_release (queue);
}