This article mainly talk about multi-threaded things, because things more, so directly take out a whole space to say it.
3, multi-threaded implementation of the bottom? 1, first say what is the process, what is a thread, what is multithreaded.
A process is a unit of resource allocation, and a thread is the basic unit of the dispatch run. Multithreading, in short, is an application in which multiple threads are executed synchronously, and a colleague who handles the processing of user interface responses and network access, usually with sub-threading, often takes more time-consuming actions, such as network access, without affecting the main thread's response to user actions in the user interface.
2. Mach
Mach is the first system to handle tasks in a multi-threaded manner, so the underlying implementation mechanism for multithreading is Mach-based threading, Mach-level threads are seldom used in development because the mach-level threads do not provide the basic features of multithreading, and the threads are independent.
3. Implement multi-threading scheme in development
1. Pthread of C language
2,OC 's nsthread
3, GCD
4, Nsoperation and Nsoperationqueue
4, how to communicate between threads?
Here to borrow this question, a more detailed review of the more commonly used three multithreaded scenarios: Nsthread, GCD, Nsoperation and Nsoperationqueue.
1, Nsthread
First look at its inter-thread communication:
1 //go to child threads to perform time-consuming tasks2 //[Self performselectorinbackground: @selector (downloadsomething:) Withobject:url];3 4 //nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (downloadsomething:) Object:url];5 // //to set the priority of a thread (0.0-1.0,1.0 highest level)6 // //thread.threadpriority = 1.0;7 //[Thread start];8 9[Nsthread detachnewthreadselector: @selector (downloadsomething:) totarget:self Withobject:url];
Three ways to go to a child thread to perform a task.
1 // back to the main thread refresh interface 2 // [Self performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no]; 3 4 // [Self.imageview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no]; 5 6 [Self.imageview performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image waitUntilDone: NO];
Three ways to refresh the interface back to the main thread after the time-consuming task has finished executing.
There are some other simple operations can go to the demo to see, there is not much to repeat.
the nsthread is lighter and more intuitive than the other two multithreaded schemes to control threading objects. But you need to manage the thread's life cycle, thread synchronization. Thread synchronization has a certain overhead in locking data.
2, GCD
GCD inter-thread communication:
1Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{2 3NSData *data =[NSData Datawithcontentsofurl:url];4NSData *data1 =[NSData DATAWITHCONTENTSOFURL:URL1];5UIImage *image =[UIImage Imagewithdata:data];6UIImage *image1 =[UIImage imagewithdata:data1];7Dispatch_async (Dispatch_get_main_queue (), ^{8Self.imageView.image =image;9Self.imageView1.image =Image1;Ten }); One A});
Download two pictures on the sub-thread and go back to the main thread update interface.
Here's a look at Dispatch Group:
The Dispatch group can be used to block a thread until all the tasks associated with the group are executed. This applies to situations where you have to wait for the task to complete before you can continue with the processing. But the following scenario in fact personally think that it is not necessary to do so, using the above way is good. This is for demo code only.
1dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);2 3Dispatch_async (Queue, ^{4dispatch_group_t Group =dispatch_group_create ();5dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);6__block UIImage *image = nil;//Note that the block assigns a value to image and must be prefixed with __block7__block UIImage *image1 =Nil;8 //Add a task to a group9Dispatch_group_async (group, queue, ^{TenNSData *data =[NSData Datawithcontentsofurl:url]; OneImage =[UIImage Imagewithdata:data]; A }); - //Add a task to a group -Dispatch_group_async (group, queue, ^{ theNSData *data1 =[NSData DATAWITHCONTENTSOFURL:URL1]; -Image1 =[UIImage imagewithdata:data1]; - }); - //wait for the task in the group to complete, return to the main thread to execute block callback +Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ -Self.imageView.image =image; +Self.imageView1.image =Image1; A }); at});
There are other uses of the following GCD, written in the demo, here will not repeat. See Nsoperation and Nsoperationqueue below.
3, Nsoperation and Nsoperationqueue
1Nsoperationqueue *queue =[[Nsoperationqueue alloc] init];2 3__block UIImage *image0 =Nil;4__block UIImage *image1 =Nil;5 6 //Create 3 Actions7Nsoperation *a = [Nsblockoperation blockoperationwithblock:^{8NSData *data =[NSData datawithcontentsofurl:url0];9IMAGE0 =[UIImage Imagewithdata:data];TenNSLog (@"current calling thread:%@", [Nsthread CurrentThread]); One }]; ANsoperation *b = [Nsblockoperation blockoperationwithblock:^{ -NSData *data =[NSData DATAWITHCONTENTSOFURL:URL1]; -Image1 =[UIImage Imagewithdata:data]; theNSLog (@"current calling thread:%@", [Nsthread CurrentThread]); - }]; -Nsoperation *c = [Nsblockoperation blockoperationwithblock:^{ - [Self.imageview performselectoronmainthread: @selector (setimage:) withobject:image0 Waituntildone:no]; + [Self.imageview1 performselectoronmainthread: @selector (setimage:) withobject:image1 Waituntildone:no]; - }]; + A //Add Dependency at [b adddependency:a]; - //[C adddependency:a]; - [C adddependency:b]; - - //Perform Actions - [Queue addoperation:a]; in [Queue addoperation:b]; -[Queue addoperation:c];
GCD and Nsoperationqueue all belong to do not need their own direct operation of the thread, I generally direct use of gcd more, one because it is efficient, and simple and convenient. However, it is convenient to use nsoperationqueue when it is necessary to execute the order as above, that is, to use the dependency.
Similarly, other about the use of nsoperation and nsoperationqueue here is not much to repeat, I hope to learn more about the demo to see the code on its own.
Demo:https://github.com/alan12138/interview-question/tree/master/4
5, How to achieve GCD internal?
1, IOS and OS X Core is the XNU kernel,GCD is based on the XNU kernel implementation
2, GCD API All in the libdispatch library
3, GCD the underlying implementation of the main Dispatch Queue and Dispatch Source
Dispatch Queue : Managing block ( operations )
Dispatch Source : Handling Events
6. Have you ever used Nsoperationqueue? If used or understood, why do you use Nsoperationqueue to achieve what? Please describe the differences and similarities between it and GCD (hint: it can be described from the implementation mechanism and scope of application).
1, GCD is a pure C language API, Nsoperationqueue is based on the GCD version of the OC package
2, GCD faster than Nsoperationqueue execution Speed
3, GCD only support FIFO queue,Nsoperationqueue can easily adjust the order of execution, set the maximum number of concurrent
4, Nsoperationqueue can be easily set up between operation dependencies, and GCD need to write a lot of code to achieve
5, Nsoperationqueue support KVO, can monitor operation is executing (isexecuted), whether the end (isfinished), whether canceled (Iscanceld )
6, How to choose : The tasks are not dependent on each other:GCD; dependencies between tasks \ or to listen to the execution of a task:nsoperationqueue
7. Since the mention of GCD, then ask what to pay attention to when using GCD and block? Are they two a thing? What is the difference between block behavior and usage in arc and the traditional MRC, and what do you need to be aware of? use of block note:
1, block of memory management (the previous topic has talked about block)
2. Prevent strong reference loops
Avoid strong reference loops:
* Non-Arc(MRC): __block (and modify external temp variable)
* ARC: __weak\__unsafe_unretained
An overview of some of the iOS questions (1)