Ios-multi-thread Nsthread Detailed

Source: Internet
Author: User
<span id="Label3"></p><p class="p1"><p class="p1"><span class="s1" style="color: #333333;">Objective</span></p></p><p class="p1"><p class="p1"><span class="s2" style="background-color: #ffffff;">a <span class="s1">thread is used to perform a task, and the thread finishes executing task <span class="s2">A to <span class="s1">perform task <span class="s2">B<span class="s1">. Multithreading is produced in order to perform two tasks at the same time. </span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">I opened a video software, I opened a thread <span class="s2">a <span class="s1">to let it perform the download task, I opened a thread <span class="s2">B<span class="s1">, used to play the Video. After I have opened two threads, these two tasks can be executed simultaneously, if I open a thread, only the download task is completed, to perform the playback Task. </span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">the thread is equivalent to the waiter in the shop, a waiter can only do one thing at the same time, more than a few waiters, you can do a lot of things at the same time. </span></span></p></p><p class="p1"><p class="p1"><span class="s1">Process</span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">1<span class="s1">, the process is an instance of application execution <span class="s2">, <span class="s1">in short, is running in the operating system Program. For example, I only open <span class="s2">QQ <span class="s1">and the two software on the phone, there will be two processes in the System. </span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">2<span class="s1">. The process cannot perform the task</span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">3<span class="s1">. The resources created by the process at run time die as the process Terminates. </span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">Threads</span></span></p></p><p class="p1"><span class="s2"><span class="Apple-tab-span">1<span class="s1">, the process itself cannot perform a task<span class="s2"><span class="s2">,<span class="s1"> process want to perform a task must be wired <span class="s2">,<span class="s1"> Thread is a separate execution unit within the process, You can only perform one task at a time, which is equivalent to a subroutine. Threads are divided into two types of <span class="s2">,<span class="s1"> main thread <span class="s2"> (<span class="s1"> user interface thread <span class="s1" ) <span> and child threads <span class="s2"> (<span class="s1"> worker thread or called background thread <span class="s2" ) <span . i am in wangjing> (<span class="s1"> operating system <span class="s2">) <span class="s1"> opened an orange product experience shop <span class="s1" span> process <span class="s2"> <span class="s1"> There are a lot of staff, there is a shop long to help me decorate the façade <span class="s2"> (<span class="s1"> main thread <span class="s2"> <span class="s1">, Consulting Staff <span class="s2"> (<span class="s1"> sub-thread <span class="s2" ) <span , sales staff> (<span class="s1"> sub-thread <span class="s1" ) <span . < span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></p><p class="p2"><p class="p2"><span class="s2"><span class="Apple-tab-span">2<span class="s1">, the thread execution will be Destroyed. </span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">3<span class="s1">. The main thread <span class="s2">( <span class="s1">also called the parent thread <span class="s2">) is <span class="s1">created and started automatically when the application starts, and is typically used to process the User's input and respond to various events and Messages. The termination of the main thread also means the end of the Program. </span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">4<span class="s1">. Child thread: created by the main <span class="s2">thread <span class="s1">to help the main thread execute the program's background processing Task. If you <span class="s2"> <span class="s1">create a child thread B in Sub-thread <span class="s2">a<span class="s1">, after it is created, the two are independent of each other and can be executed concurrently between multiple child threads. </span></span></span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">5<span class="s1">. There can be multiple threads in a process, and all threads are in the virtual address space of the process and can use the global variables and system resources of the PROCESS. </span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">6.<span class="s1">Thread state: Five states of thread</span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">Multithreading</span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">1<span class="s1">, most of the current <span class="s2">apps, <span class="s1">need to connect to the server, and access to the server may be fast or slow. If the <span class="s2">operation of an app <span class="s1">access server does not operate on a child thread, <span class="s2">the <span class="s1">software cannot respond to the User's actions during the App's access to the server, only after the app has been <span class="s2"> <span class="s1">accessed, the<span class="s2">app <span class="s1">in order to respond to user actions, which leads to thread blocking, which is a common lag phenomenon. A thread can only perform one task at a time <span class="s2">, <span class="s1">but a process may have multiple threads. Multiple threads can be opened to perform different tasks <span class="s2">, <span class="s1">thus improving the execution efficiency of the program and avoiding thread blocking. </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p></p><p class="p2"><p class="p2"><span class="s2"><span class="Apple-tab-span">2<span class="s1">, the operating system will be based on the priority of the thread <span class="s2">( <span class="s1">thread priority can be set manually <span class="s2">) <span class="s1">to schedule the <span class="s2">CPU <span class="s1">time, High priority threads, the chance of priority calls will be greater, peer, look at the thread execution successively. </span></span></span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">3<span class="s1">, at the same time, the<span class="s2">CPU <span class="s1">can only process one thread, only one thread is Working. Multithreading parallel execution, in fact, each thread is constantly switching, because the time to perform the switchover quickly, resulting in the illusion of simultaneous execution, the principle of such as <span class="s2">a<span class="s1">,<span class="s2">B <span class="s1">two threads;</span> </span> </span> </span> </span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2">A <span class="s1">execution to a certain time period to switch, but <span class="s2">a <span class="s1">task is not completed, the system will be <span class="s2">a <span class="s1">current execution of the location and data in the way to save</span> </span> </span> </span> </span> it in a stack</span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">then the <span class="s2">b <span class="s1">thread executes,<span class="s2">b <span class="s1">Execution time is up, and its position status is saved to the stack of <span class="s2">b <span class="s1">. </span></span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="Apple-tab-span">4<span class="s1">, the system automatically find <span class="s2">a <span class="s1">stack, will <span class="s2">be <span class="s1">saved before a data recovery, but also from <span class="s2">a <span class="s1">before the state of disconnection to continue to execute, so loop, the system each open a thread has a relatively large overhead. If the thread is too open, it will not only take up a lot of memory and make the program more complex, but also aggravate <span class="s2">the burden of the CPU <span class="s1">, such software will make your mobile phone in the winter into a warm hand treasure. </span></span></span></span></span></span></span></span></span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"></span> Why <span style="line-height: 1.5;">(</span> <span class="s1" style="line-height: 1.5;">How to use multithreading <span class="s2">)</span> </span></p></p><p class="p2"><p class="p2"><span style="line-height: 1.5;">Improve program execution efficiency and avoid lag caused by thread blocking.</span></p></p><p class="p2"><p class="p2"><span class="s1"></span> <span class="s2"> <span class="s1">can appropriately improve resource utilization <span class="s2">(CPU, <span class="s1">memory <span class="s2">)<span class="s1">. </span></span></span></span></span></span></p></p><p class="p2"><p class="p2"><span class="s1"></span> <span style="line-height: 1.5;">non-abusive multithreading:</span></p></p><p class="p1"><p class="p1"><span style="line-height: 1.5;">Open threads need to occupy a certain amount of memory (by default, The main thread occupies</span> <span class="s2" style="line-height: 1.5;">1M<span class="s1">, sub-threads occupy <span class="s2">512KB<span class="s1">, can set their own memory size, but must be a <span class="s2">multiple of 4 <span class="s1">, if you open a large number of threads, will consume a lot of memory space, reduce the performance of the program</span> </span> </span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2">The more threads, the greater the <span class="s1"><span class="s2">overhead of the CPU on the <span class="s1">dispatch thread</span> </span> </span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">programming is more complex: such as communication between threads, multi-threaded data sharing</span></span></p></p><p class="p2"><p class="p2"><span style="font-size: 18px; font-family: 宋体;"><strong><span style="line-height: 1.5;">Summarize</span></strong></span></p></p><p class="p2"><p class="p2"><span style="line-height: 1.5;"></span> <span class="s1" style="line-height: 1.5;">thread-to-process Relationships</span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">a thread is the <span class="s2"> <span class="s1">basic unit of CPU execution, a process can have multiple threads, but can only perform one task at a Time. </span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s2">the <span class="s1">process is the running software, is Dynamic. </span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">an operating system can correspond to multiple processes <span class="s2">, <span class="s1">a process can have multiple threads <span class="s2">, <span class="s1">but at least one thread</span> </span> </span> </span> </span></span></p></p><p class="p1"><p class="p1"><span class="s2"><span class="s1">threads in the same process share resources in a process</span></span></p></p><br><span style="color: #ff6600;"><span style="color: #ff6600;"><strong>Nsthread Common Methods</strong></span></span> <ul> <ul> <li>There are two ways to use Nsthread to open threads:</li> </ul> </ul> <ol> <li><li><p class="p1"><span class="s1">Create a manual open mode</span></p><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">Object</span>:<span style="color: #800000;">@ "</span><span style="color: #800000;">thread</span><span style="color: #800000;">"</span><span style="color: #000000;">];</span> <span style="color: #008080;">2</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Open Thread</span> <span style="color: #008080;">3</span> [thread start];</pre><p></p></li></li> <li><li>Create and Auto-open mode<pre><span style="color: #008080;">1</span> <span style="color: #008000;">/*</span> <span style="color: #008080;">2</span> <span style="color: #008000;"> Create and Auto-open mode</span> <span style="color: #008080;">3</span> <span style="color: #008000;">*/</span> <span style="color: #008080;">4</span> [nsthread detachnewthreadselector: @selector (thread1:) totarget:self withobject:<span style="color: #800000;">@ "</span><span style="color: #800000;">thread1</span> <span style="color: #800000;">"</span>];</pre></li></li> </ol><p><p></p></p><p class="p1"><p class="p1"><strong>Thread classification</strong></p></p><p class="p1"><p class="p1"><span class="s1">1<span class="s3">> <span class="s2">main thread <span class="s3"> <span class="s2">:<span class="s3"> UI <span class="s2">thread, display, Refresh <span class="s3">UI <span class="s2">interface, handle <span class="s3"> <span class="s2">events for UI controls</span> </span> </span> </span> </span></span></span></span></span></span></span></p></p><p class="p1"><p class="p1"><span class="s1">2<span class="s3">> <span class="s2">Child Threads <span class="s3"> <span class="s2">:<span class="s3"> <span class="s2">background threads, asynchronous Threads</span> </span> </span> </span> </span> </span></span></p></p><p class="p1"><p class="p1"><span class="s1">Note: do not place time-consuming operations on the main thread, but in sub-threads, because the main thread contains the user interface, so it is important to reduce the time-consuming operation of the main thread to avoid challenging the User's Patience.</span></p></p><p class="li1"><p class="li1"><strong>Nsthread Common operations</strong></p></p> <ul> <ul> <li class="li1">Creating and starting Threads</li> </ul> </ul><pre><pre>Nsthread *thread = [[nsthread alloc] initwithtarget:self selector: @selector (run) object:nil]; [thread Start];//thread starts, It executes the run method of self in Threads thread</pre></pre> <ul> <ul> <li class="li1">Main thread Correlation method</li> </ul> </ul><pre><pre>+ (nsthread *) mainthread; Get the main thread-(BOOL) ismainthread; is the main thread + (BOOL) ismainthread; is the main thread</pre></pre> <ul> <ul> <li class="li1"><span class="s1">Get current thread</span></li> </ul> </ul><pre><pre>Nsthread *current = [nsthread currentthread];</pre></pre> <ul> <ul> <li class="li1"><span class="s1">Scheduling Priority for Threads</span></li> </ul> </ul><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>+ (double) threadpriority;+ (bool) setthreadpriority: (double) p;-(double) threadpriority;-(bool) setthreadpriority: ( Double) p;</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p class="p4"><p class="p4"><span class="s6">The value range of the scheduling priority <span class="s2">is <span class="s7">0.0 ~ 1.0<span class="s2">, the default <span class="s7">0.5, the higher<span class="s2">the value, the higher the priority level</span> </span> </span> </span> </span></span></p></p><p class="p3"><p class="p3"><span class="s2"></span></p></p> <ul> <ul> <li class="li1"><span class="s1">The name of the thread</span></li> </ul> </ul><pre><pre>-(void) setName: (nsstring *) n;-(nsstring *) name;</pre></pre> <ul> <ul> <li class="li1"><span class="s1">Automatically start a thread after creating a thread</span></li> </ul> </ul><pre><pre>[nsthread detachnewthreadselector: @selector (run) totarget:self withobject:nil];</pre></pre> <ul> <ul> <li class="li1"><span class="s1">Implicitly creating and starting a thread</span></li> </ul> </ul><pre><pre>[self performselectorinbackground: @selector (run) withobject:nil]; <span class="s2"> </span></pre></pre><p class="li1"><p class="li1"><span class="s1"><span class="s2"><span class="s6"> <span class="s2">Pros and cons of the above 2 ways to create threads</span> </span></span></span></p></p> <ul> <ul> <li class="li1"><span class="s7">Advantages: Simple and fast</span></li> <li class="li4"><span class="s7">Cons: Unable to set the thread in more detail</span></li> </ul> </ul><p class="li4"><p class="li4"><strong>The state of the thread</strong></p></p> <ol> <ol> <li class="li4"><strong>Possible state evolution of threads in a program</strong></li> </ol> </ol><p class="li4"><p class="li4"><strong>    </strong>For example, show the state of a thread at different times</p></p><p class="li4"><p class="li4"></p></p><p class="li4"><p class="li4"></p></p><p class="li4"><p class="li4">  <strong>2. How to control thread state</strong></p></p> <ul> <ul> <li class="li1"><span class="s1">Start thread</span></li> </ul> </ul><pre><pre>-(void) start; Go to Ready status, run Status. Automatically enters the death state when the thread task is completed</pre></pre> <ul> <ul> <li class="li1"><span class="s1">Blocking (pausing) Threads</span></li> </ul> </ul><pre><pre>+ (void) sleepuntildate: (nsdate *) date;+ (void) sleepfortimeinterval: (nstimeinterval) ti;<br><span class="s2">Enter blocking State</span></pre></pre> <ul> <ul> <li class="li1"><span class="s1">Force Stop Thread</span></li> </ul> </ul><pre><pre>+ (void) Exit;//into death state</pre></pre><p class="p6"><p class="p6"><span class="s2">Note: once the thread has stopped (dead), it cannot be opened Again.</span></p></p><p class="p6"><p class="p6"></p></p><p class="p6"><p class="p6"><strong>The security hidden trouble of multithreading</strong></p></p> <ol> <ol> <li class="li1"><strong>Resource sharing</strong></li> </ol> </ol> <ul> <ul> <li class="li1"><span class="s3"><span class="s4">1 <span class="s2">of resources may be shared by multiple threads, that is, <span class="s5">multiple threads may access the same</span> block of resources</span></span></span></li> <li class="li1"><span class="s3">For example, multiple threads access the same object, the same variable, the same file</span></li> <li class="li1"><span class="s5">Data confusion and data security <span class="s2">issues</span> </span> can easily arise when multiple threads access the same resource</li> </ul> </ul><p class="li1"><p class="li1">As shown in the Thread safety vulnerability analysis:</p></p><p class="li1"><p class="li1"></p></p><p class="li1"><p class="li1"></p></p><p class="li1"><p class="li1">To solve the problem, we can use the mutex technique in nsthread, as shown in:</p></p><p class="li1"><p class="li1"></p></p><p class="p1"><p class="p1"><strong>Security hazard resolution – Mutual exclusion lock</strong></p></p> <ul> <ul> <li class="li1"><span class="s1">Mutex use format</span></li> </ul> </ul><pre><pre>@synchronized (lock Object) {//code to be locked }</pre></pre><p class="p3"><p class="p3"><span class="s2">Note: locking <span class="s7">1 <span class="s2">parts of code with only <span class="s7">1 <span class="s2">locks, with multiple locks is not valid</span> </span> </span> </span></span></p></p><p class="p4"><p class="p4"><span class="s2"></span></p></p> <ul> <ul> <li class="li1"><span class="s8">Advantages and disadvantages of mutual exclusion lock</span></li> </ul> </ul><p class="li1"><p class="li1"><span class="s9">Advantages: it can effectively prevent the data security problem caused by multi-thread snatch resource</span></p></p><p class="li1"><p class="li1"><span class="s9"><span class="s2">Cons: consumes a lot of <span class="s7">CPU <span class="s2">Resources</span> </span></span></span></p></p> <ul> <ul> <li class="li1"><span class="s9">Use of mutexes: multiple threads rob the same piece of resources</span></li> </ul> </ul> <ul> <ul> <li class="li1"><span class="s9"><span class="s2">Related professional terminology: <span class="s10">thread synchronization</span></span></span></li> </ul> </ul><p class="li1"><p class="li1"><span class="s8">Thread synchronization means that multiple threads are executing on the same line (performing tasks Sequentially)</span></p></p><p class="li6"><p class="li6"><span class="s9">Mutex is the use of thread synchronization technology</span></p></p><p class="p1"><p class="p1"><strong>Atomic and Non-atomic Properties</strong></p></p> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">OC <span class="s3">has <span class="s4">nonatomic <span class="s3">and <span class="s4">atomic <span class="s3">two choices</span> </span> </span> </span> when defining attributes</span></span></span></li> </ul> </ul><p class="li2"><p class="li2"><span class="s5"><span class="s6">Atomic<span class="s4">: Atomic attribute, <span class="s7">locking for Setter <span class="s4">method (default is <span class="s6">atomic<span class="s4">)</span> </span> </span> </span> </span></span></span></p></p><p class="li3"><p class="li3"><span class="s8"><span class="s6">Nonatomic<span class="s4">: non-atomic attribute, does <span class="s7">not <span class="s4">lock</span> the setter method</span> </span></span></span></p></p> <ul> <ul> <li class="li3"><p class="p1"><span class="s1">Selection of atomic and Non-atomic properties</span></p></li> </ul> </ul><p class="p1"><p class="p1"><span class="s1"><span class="s2"><span class="s3"> <span class="s2"> <span class="s3">comparison</span> </span> of nonatomic and Atomic</span></span></span></p></p> <ul> <ul> <li> <ul> <li> <ul> <li class="li2"><span class="s4"><span class="s5">Atomic<span class="s2">: thread-safe, requires a lot of resources</span></span></span></li> <li class="li2"><span class="s4"><span class="s5">Nonatomic<span class="s2">: non-thread-safe, Suitable for small memory mobile devices</span></span></span></li> </ul></li> </ul></li> </ul> </ul><p class="li2"><p class="li2"><span class="s6"><span class="s7"><span class="s2">Recommendations for 2.iOS development</span></span></span></p></p> <ul> <ul> <li> <ul> <li> <ul> <li class="li2"><span class="s6"><span class="s8">All properties are declared as <span class="s9">nonatomic</span></span></span></li> <li class="li2"><span class="s6">Try to avoid multiple threads robbing the same piece of resources</span></li> <li class="li5"><span class="s6">As far as possible to locking, resource-grabbing business logic to the Server-side processing, reduce the pressure of mobile clients</span></li> </ul></li> </ul></li> </ul> </ul><p class="li5"><p class="li5"></p></p><p class="p1"><p class="p1"><span class="s1">Inter-thread Communication</span></p></p> <ul> <ul> <li class="li1"><span class="s1">What is called Inter-thread communication</span></li> </ul> </ul><p class="li1"><p class="li1"><span class="s3"><span class="s2">In <span class="s4">1 <span class="s2">processes, threads often do not exist in isolation and require frequent communication between multiple threads</span> </span></span></span></p></p> <ul> <ul> <li class="li1"><span class="s3">The embodiment of inter-thread communication</span></li> </ul> </ul><p class="li1"><p class="li1"><span class="s3"><span class="s4">1 <span class="s2">threads Pass data to another <span class="s4">1 <span class="s2">threads</span> </span> </span></span></span></p></p><p class="li1"><p class="li1"><span class="s3"><span class="s2"><span class="s4"> <span class="s2">After performing a specific task in 1 threads, go to another <span class="s4">1 <span class="s2">thread to continue the task</span> </span> </span> </span></span></span></p></p> <ul> <ul> <li class="li1"><span class="s3">Common methods for inter-thread communication</span></li> </ul> </ul><pre><pre>-(void) performselectoronmainthread: (SEL) aselector withobject: (id) arg waituntildone: (BOOL) wait;-(void) Performselector: (SEL) aselector onthread: (nsthread *) thr withobject: (id) arg waituntildone: (BOOL) wait;</pre></pre> <ul> <ul> <li class="p1"><span class="s1">Example of inter-thread communication – image download</span></li> </ul> </ul><p class="p1"><p class="p1"><span class="s1"></span></p></p><p class="p2"><p class="p2"><span class="s1"></span></p></p><p><p>Ios-multi-thread Nsthread Detailed</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.