concurrency : Multiple tasks are run in the same time. Also called the common line, refers to the ability to deal with multiple simultaneous sexual activities.
parallel : means that two concurrent tasks occur at the same time.
Concurrency is not necessarily parallel, and concurrency does not have to occur concurrently.
As an example:
There are two express delivery to the destination, the following two kinds of options:
(1) a courier sent two courier to their respective destinations. Concurrent
(2) Two couriers send a courier to the destination at the same time. Parallel
In iOS, you can often see that there is a need for a method to wait for another method to perform the corresponding processing, such as some network requests, need to be based on the return value of the previous request to do the corresponding processing and then execute the second request, so we can not allow two requests to request the network at the same time. The following records control concurrency through GCD and nsoperationqueue.
Dispatch_semaphore
The semaphore is an integer value and has an initial count value, and the semaphore typically supports two operations: notification and wait. The count value increases when the signal is notified, and when the semaphore is waiting on the thread, the thread is blocked until the signal is notified that the count is greater than 0, and then the threads reduce the count to continue working.
GCD 3 semaphores in the operation:
Dispatch_semaphore_create Semaphore creation
Dispatch_semaphore_signal Sending notifications
Dispatch_semaphore_wait semaphore wait
__block dispatch_semaphore_t sem = dispatch_semaphore_create (0); dispatch_queue_t Queue= Dispatch_queue_create ("Testblock", NULL); Dispatch_async (Queue,^{ for(inti =0; I < -; i++) {NSLog (@"The value of I is:%d", i); } dispatch_semaphore_signal (SEM); }); Dispatch_semaphore_wait (SEM, dispatch_time_forever); for(intj =0; J <Ten; J + +) {NSLog (@"The value of J is:%d", J); }
The operating result is:
Nsoperationqueue
In the case of not adding dependencies:
Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; Queue.maxconcurrentoperationcount=Ten; Nsblockoperation*operation1 = [Nsblockoperation blockoperationwithblock:^{ for(inti =0; I < +; i++) {NSLog (@"Execute concurrent Queue 1:%d", i); } }]; Nsblockoperation*operation2 = [Nsblockoperation blockoperationwithblock:^(){ for(inti =0; I < the; i++) {NSLog (@"Execute concurrent Queue 2:%d", i); } }]; [Queue Addoperation:operation1]; [Queue Addoperation:operation2];
Operation Result:
Two nsoperation are printed concurrently.
After adding dependency control:
Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; Queue.maxconcurrentoperationcount=Ten; Nsblockoperation*operation1 = [Nsblockoperation blockoperationwithblock:^{ for(inti =0; I < +; i++) {NSLog (@"Execute concurrent Queue 1:%d", i); } }]; Nsblockoperation*operation2 = [Nsblockoperation blockoperationwithblock:^(){ for(inti =0; I < the; i++) {NSLog (@"Execute concurrent Queue 2:%d", i); } }]; [Operation1 Adddependency:operation2]; Add dependency, operation1 to rely on operation2 to continue execution [queue Addoperation:operation1]; [Queue Addoperation:operation2];
Printed results: