/*
It's not a technical article, just a record of how you learn every day.
----------------------------Program Ape's Journey is the Sea of stars
*/
[1] What is multithreading?
Before the multi-threaded learning, many people will process, thread, asynchronous, synchronous, serial, concurrent confused, the concept of confusion is very serious, even in the use of some third-party libraries, do not know how it is used for data requests, asynchronous or synchronous, multi-threaded or single-threaded. Here I summed up a few days to contact the multi-threaded knowledge, the concept of the content to clarify.
process : In an iOS system, you often use tasks such as one application as a process.
1 processes to perform a task, a thread must be wired (at least 1 threads per 1 processes)
thread : The constituent unit of a process , which is a block of code repeatedly used in a process
Threads are the basic unit of execution of a process, and all tasks of a process (program) are executed in the thread
For example, using a cool dog to play music, using Thunder to download movies, all need to execute in the thread
Feature: A thread can only perform the same task at the same time, if multiple tasks are assigned to the same task to complete,
The thread then executes a task and then executes the next task until the task is fully completed.
"Multithreading Interpretation":
Based on the characteristics of processes and threads, we can open up multiple threads in one process, and they can perform different tasks in parallel (that is, time synchronized), like a process is a project group and a thread is a member of a project group. Multiple threads can perform different tasks at the same time, just like project team members can have different tasks at the same time, such as Art and program apes have different projects.
What we need to note is: in the single core era: The CPU cannot execute multiple threads at the same time! wtf!
Although it does not sound acceptable, it is true that the CPU can only execute one thread at a time, and it seems that multiple threads can work concurrently, because the CPU switches back and forth between multiple threads, and if the CPU is fast enough to switch back and forth, it creates the illusion of multi-threaded concurrency execution.
Multi-core era, the kernel-level structure using a preemptive approach, the next thread in the idle kernel, here is not too much to repeat, whether it is a single core or multi-core approach, multithreading will improve the CPU work requirements.
Because of this reason, too many threads, CPU resources will be too much occupied, processing data and resources will be slow, so try to control the number of multiple threads in a process. (The default memory consumption is 1M for the main thread, and the child threads occupy 512KB)
Multi-Threading Advantages: It can improve the execution efficiency of the program, and can improve the utilization of resources appropriately .
The disadvantage of multithreading: both the start and exit of a thread require memory space to support, and the complexity of the program is increased.
Not conducive to reading comprehension, the more threads, the overall process of the CPU overhead is greater.
The use of multithreading requires more attention to this aspect of content, can not be too casual to open new threads.
[2] main thread and secondary threads (worker threads)
Main thread: In each process there is a main thread, all UI control building and gesture drag and view scrolling, etc. are implemented in the main thread.
We often need to carry out some time-consuming work, such as data requests, loop fetch data or processing content, these times, the main thread will be tied to the entire view screen can not be clicked, the situation of suspended animation, which is fatal to the user experience, we in order to avoid such situations, often take a thread to develop the method
Sub-thread: Also known as a worker thread, the main role is to share the primary thread of the task, and can prevent the screen suspended animation accident.
[3] Serial and parallel:
Serial: When you use a thread to process multiple tasks, you must perform them in order.
Concurrency: Multiple threads perform different tasks synchronously.
[4] Nsthread Simple turn on multithreading
For Nsthread, it is a simple way to turn on multithreaded mode. By instantiating its object on the main thread and executing the Start method (the Nsthread object is automatically executed by the class method initialization), a single thread can be implemented simply.
<1> Simple implementation of multithreading examples
//Create two buttons in a view-(void) creatuibutton{Nsarray* [Email protected][@"Thread 1",@"Thread 2"]; for(intI=0;i<2; i++) {UIButton* btn=[UIButton Buttonwithtype:uibuttontypecustom]; Btn.frame=cgrectmake ( the, -+ -(In -, -); Btn.tag=i+1; Btn.backgroundcolor=[Uicolor Graycolor]; [BTN Settitle:arr[i] forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Cyancolor] forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (PRESSBTN:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; }}//Perform a button click event-(void) Pressbtn: (ID) sender{//Building sub-threadsUIButton * btn = (UIButton *) sender; //Multiple threads Concurrent work does not affect each other if(btn.tag==1){ //1?? class method to create a thread object /*parameter explanation: The first parameter is the Selector method The second parameter is the method responder the third object is the passed parameter, which is the OC object*/NSNumber* [Email protected]Ten; [Nsthread detachnewthreadselector: @selector (threadMain1:) totarget:self Withobject:num]; //using a class method to create a thread object, you do not need to manually open the thread, and the thread method is automatically called as long as the thread is created. }Else{NSNumber* [Email protected]Ten; //2?? Use an instance method to create a thread object, but you must manually turn on the threadNsthread * thread= [[nsthread alloc]initwithtarget:self selector: @selector (threadMain2:)Object: num]; //manually turn on threads[Thread start]; }}//thread 1 Code Execution-(void) ThreadMain1: (NSNumber *) num{NSLog (@"thread 1 start >>>>>>>>"); for(intI=0; I<[num Intvalue]; i++) {NSLog (@"Thread 1 >>>> i =%d", i); [Nsthread sleepfortimeinterval:0.1 ]; //thread sleeps for 0.5 seconds} NSLog (@"thread 1 ends");//The end of the thread exits immediately (disappears)}//thread 2 Code execution-(void) ThreadMain2: (NSNumber *) num{NSLog (@"thread 2 starts!-------------!"); for(intI=0; I<[num Intvalue]; i++) {NSLog (@"Thread 2 i =%d", i* +); [Nsthread sleepfortimeinterval:0.3]; //thread sleeps for 0.5 seconds} NSLog (@"thread 2 ends"); }
-------in this instance we click on the thread one button and then immediately click on the Thread 2 button
What happens if the two click events are executed in the main thread?
This must be clear to many people, after clicking the first button, then click the Second button, their click events are in sequence.
Button 2 's Click event does not respond when the Click event for button 1 is not finished.
So what happens if two click events are executed in the secondary thread?
Let's take a look at the results of this instance
It can be seen that the online Cheng Click event is not over, click on the Thread 2 button, the program immediately opened the thread 2 method. Therefore, we can know:
multithreading does not interfere with each other, as long as the main thread to open up a sub-threading, it can independently complete the tasks you have scheduled. We can also print some iconic content in the main thread, and it can be seen that the task of the main thread is not terminated because the secondary thread is in progress, it continues to execute its own code instead of waiting for the secondary thread to end.
iOS multithreaded management <1> about multithreading you have to know two or three things