iOS multi-thread nsthread use

Source: Internet
Author: User
<span id="Label3"></p><p class="p1"><p class="p1"><span style="font-size: 16px;"><strong>multi-threading Technology in iOS</strong></span></p></p><p class="li1"><p class="li1"><span style="font-size: 16px;"><strong>  </strong><span style="font-size: 14px;">In our iOS development project, we used multithreaded technology in order to solve the UI operation not being blocked by time-consuming operations. In iOS development, we mainly use three kinds of multithreading technology: NSTHREAD,NSOPERATION,GCD.</span></span></p></p><p class="li1"><p class="li1"><span style="font-size: 16px;"><span style="font-size: 14px;">1.<strong>thread</strong> : Three methods are relatively lightweight, but need to manage the life cycle of the thread, synchronization, lock issues, which can lead to a certain performance cost<br>2.<strong>Cocoa Operations</strong>: Based on OC implementations, Nsoperation encapsulates the actions that need to be performed in an object-oriented manner, without concern for thread management, synchronization, and so On. Nsoperation is an abstract base class, and iOS provides two default implementations: nsinvocationoperation and nsblockoperation, and of course you can customize nsoperation<br>3.<strong>Grand Central Dispatch</strong>(gcd,ios4 only started): provides new features, runtime to support multicore parallel programming, with a higher focus: how to increase efficiency on multiple CPUs</span></span></p></p><p class="li1"><p class="li1"><span style="font-size: 16px;"><span style="font-size: 14px;">This article mainly nsthread the first multithreaded operation Technology.</span></span></p></p><p class="p1"><p class="p1"><span style="font-size: 16px;"><strong>Thread classification</strong></span></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"><span style="font-size: 16px;"><strong>Nsthread Common operations</strong></span></p></p> <ul> <ul> <li class="li1">Creating and starting Threads</li> </ul> </ul><pre><pre><span style="color: #0000ff;">Object</span> <span style="color: #000000;">: nil]; [thread start];</span> <span style="color: #008000;">//</span> <span style="color: #008000;">when a thread is started, the Run method of self is executed in threads thread</span></pre></pre> <ul> <ul> <li class="li1">Main thread Correlation method</li> </ul> </ul><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">get the main</span> thread <span style="color: #008000;">//</span> is <span style="color: #008000;">the main</span> thread <span style="color: #008000;">//</span> is <span style="color: #008000;">the main</span> thread</pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Get current thread</span></span></li> </ul> </ul><pre><pre>Nsthread *current = [nsthread currentthread];</pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Scheduling Priority for Threads</span></span></li> </ul> </ul><pre><pre>+ (<span style="color: #0000ff;">double</span><span style="color: #000000;">) threadpriority;</span> + (BOOL) setthreadpriority: (<span style="color: #0000ff;">double</span><span style="color: #000000;">) p;</span> -(<span style="color: #0000ff;">Double</span><span style="color: #000000;">) threadpriority;</span> -(BOOL) setthreadpriority: (<span style="color: #0000ff;">double</span>) p;</pre></pre><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"><span class="s2">The name of the thread</span></span></li> </ul> </ul><pre><pre>-(<span style="color: #0000ff;">void</span>) setName: (nsstring *<span style="color: #000000;">) n;</span> -(nsstring *) name;</pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Automatically start a thread after creating a thread</span></span></li> </ul> </ul><pre><pre>[nsthread detachnewthreadselector: @selector (run) totarget:self withobject:nil];</pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Implicitly creating and starting a thread</span></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"><span class="s2">Advantages: Simple and fast</span></span></li> <li class="li4"><span class="s7"><span class="s2">Cons: Unable to set the thread in more detail</span></span></li> </ul> </ul><p class="li4"><p class="li4"><span style="font-size: 16px;"><strong>The state of the thread</strong></span></p></p> <ol> <ol> <li class="li4"><strong><span style="font-size: 14px;">Possible state evolution of threads in a program</span></strong></li> </ol> </ol><p class="li4"><p class="li4"><span style="font-size: 16px;"><strong>    </strong><span style="font-size: 14px;">For example, show the state of a thread at different times</span></span></p></p><p class="li4"><p class="li4"><span style="font-size: 16px;"><span style="font-size: 14px;"></span></span></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"><span class="s2">Start thread</span></span></li> </ul> </ul><pre><pre>-(<span style="color: #0000ff;">void</span><span style="color: #000000;"></span><span style="color: #008000;">//</span> <span style="color: #008000;"> Enter ready status, run Status.) Automatically enters the death state when the thread task is completed</span></pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Blocking (pausing) Threads</span></span></li> </ul> </ul><pre><pre>+ (<span style="color: #0000ff;">void</span>) sleepuntildate: (nsdate *<span style="color: #000000;">) date;</span> + (<span style="color: #0000ff;">void</span>) sleepfortimeinterval: (nstimeinterval) ti;<br><span class="s2">Enter blocking State</span></pre></pre> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Force Stop Thread</span></span></li> </ul> </ul><pre><pre>+ (<span style="color: #0000ff;">void</span><span style="color: #000000;">) exit;</span> <span style="color: #008000;">//</span> <span style="color: #008000;">into the state of death</span></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"><span style="font-size: 16px;"><strong>The security hidden trouble of multithreading</strong></span></p></p> <ol> <ol> <li class="li1"><strong><span class="s1"><span class="s2">Resource sharing</span></span></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"><span class="s2">For example, multiple threads access the same object, the same variable, the same file</span></span></li> <li class="li1"><span style="line-height: 1.5;">When multiple threads access the same piece of resources, it is easy to throw</span> <span class="s5" style="line-height: 1.5;">data confusion and data security <span class="s2">issues</span> </span></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"><span style="font-size: 16px;"><strong><span class="s1">Security hazard resolution – Mutual exclusion lock</span></strong></span></p></p> <ul> <ul> <li class="li1"><span class="s1"><span class="s2">Mutex use format</span></span></li> </ul> </ul><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">code that needs to be locked }</span></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"><span class="s2">Advantages and disadvantages of mutual exclusion lock</span></span></li> </ul> </ul><p class="li1"><p class="li1"><span class="s9"><span class="s2">Advantages: it can effectively prevent the data security problem caused by multi-thread snatch resource</span></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"><span class="s2">Use of mutexes: multiple threads rob the same piece of resources</span></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"><span class="s2">Thread synchronization means that multiple threads are executing on the same line (performing tasks Sequentially)</span></span></p></p><p class="li6"><p class="li6"><span class="s9"><span class="s2">Mutex is the use of thread synchronization technology</span></span></p></p><p class="p1"><p class="p1"><span style="font-size: 16px;"><strong><span class="s1">Atomic and Non-atomic Properties</span></strong></span></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 style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <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 style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <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"><span class="s2">Try to avoid multiple threads robbing the same piece of resources</span></span></li> <li class="li5"><span class="s6"><span class="s2">As far as possible to locking, resource-grabbing business logic to the Server-side processing, reduce the pressure of mobile clients</span></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"><span class="s2">What is called Inter-thread communication</span></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"><span class="s2">The embodiment of inter-thread communication</span></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"><span class="s2">Common methods for inter-thread communication</span></span></li> </ul> </ul><pre><pre>-(<span style="color: #0000ff;">void</span>) performselectoronmainthread: (SEL) aselector withobject: (<span style="color: #0000ff;">ID</span><span style="color: #000000;">) arg waituntildone: ( BOOL) wait;</span> -(<span style="color: #0000ff;">void</span>) performselector: (SEL) aselector onthread: (nsthread *) thr withobject: (<span style="color: #0000ff;">ID</span>) 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="p3"><p class="p3"></p></p><p class="li5"><p class="li5"></p></p><p><p>iOS multi-thread nsthread use</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.