iOS development multithreading-related Concepts of multithreading (1)
Last Update:2016-05-13
Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica"><strong>IOS</strong></span> <strong>Developing multithreaded articles</strong> <span style="line-height:normal; font-family:Helvetica"><strong>-</strong></span> <strong>Multithreading Simple Introduction</strong></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC Semibold‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC Semibold‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:‘PingFang SC‘"><strong>I. Processes and Threads</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">1.</span> What is a process</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">A process is an application that is running in the system</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Each process is independent, and each process runs within its dedicated and protected memory space</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">For example, Open <span style="line-height:normal; font-family:Helvetica">QQ</span>,<span style="line-height:normal; font-family:Helvetica">Xcode</span>, the system will start <span style="line-height:normal; font-family:Helvetica">2</span> processes respectively</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">You <span style="line-height:normal; font-family:Helvetica"></span> can view the <span style="line-height:normal; font-family:Helvetica"></span> processes that are open in <span style="line-height:normal; font-family:Helvetica">your MAC</span> system through Activity Monitor</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">2.</span> What is a thread</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">1</span> processes to perform a task, a thread must be wired (at least <span style="line-height:normal; font-family:Helvetica">1</span> threads per <span style="line-height:normal; font-family:Helvetica">1</span> processes)</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Threads are the basic unit of execution of a process, and all tasks of a process (program) are executed in the thread</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">For example, using a cool dog to play music, using Thunder to download movies, all need to execute in the thread</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">3.</span> Thread's serial</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><span style="line-height:normal">1</span> the <span style="font-family:‘PingFang SC‘">execution of a task in a thread is serial</span></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">If you want to perform multiple tasks in <span style="line-height:normal; font-family:Helvetica">1</span> threads, You can perform these tasks sequentially, one at a-</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">That is,<span style="line-height:normal; font-family:Helvetica">1</span> threads can only perform <span style="line-height:normal; font-family:Helvetica">1</span> tasks at a time</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">For example, download <span style="line-height:normal; font-family:Helvetica">3</span> files in <span style="line-height:normal; font-family:Helvetica">1</span> threads (file <span style="line-height:normal; font-family:Helvetica">a</span>, file <span style="line-height:normal; font-family:Helvetica">B</span>, file <span style="line-height:normal; font-family:Helvetica">C</span>, respectively)<span style="font-family:Helvetica"> </span></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="font-family:Helvetica"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">second, multi-threading</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">1.</span> What is multithreading</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Multiple threads can be opened in <span style="line-height:normal; font-family:Helvetica">1</span> processes, each thread can perform different tasks in parallel (at the same time)</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Process <span style="line-height:normal; font-family:Helvetica"></span> , workshop, thread <span style="line-height:normal; font-family:Helvetica"></span> -shop workers</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Multithreading technology can improve the efficiency of program execution</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">For example, the simultaneous opening of <span style="line-height:normal; font-family:Helvetica">3</span> threads to download <span style="line-height:normal; font-family:Helvetica">3</span> files (file <span style="line-height:normal; font-family:Helvetica">a</span>, file <span style="line-height:normal; font-family:Helvetica">B</span>, file <span style="line-height:normal; font-family:Helvetica">C</span>, respectively)</p></p><p style="margin-top:8px; margin-bottom:8px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:8px; margin-bottom:8px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><span style="font-size:18px"></span><br></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">2.</span> multi-threading principle</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">At the same time, the<span style="line-height:normal; font-family:Helvetica">CPU</span> can only handle <span style="line-height:normal; font-family:Helvetica">1</span> threads, and only <span style="line-height:normal; font-family:Helvetica">1</span> threads are working (executing)</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Multi-threaded concurrency (simultaneous) execution, in fact, the <span style="line-height:normal; font-family:Helvetica">CPU</span> is quickly dispatched between multiple threads (switching)</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">If the <span style="line-height:normal; font-family:Helvetica">CPU</span> schedules Threads Fast enough, It creates the illusion of multi-threaded concurrency execution</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Think: What happens if the threads are very, very much?</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">CPU</span> will be dispatched between <span style="line-height:normal; font-family:Helvetica">N</span> multithreading, The<span style="line-height:normal; font-family:Helvetica">CPU</span> will be exhausted, consumes a lot of <span style="line-height:normal; font-family:Helvetica">CPU</span> Resources</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">The frequency at which each thread is scheduled to execute is reduced (thread execution efficiency is reduced)</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">3.</span> Advantages and disadvantages of multi-threading</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Advantages of multithreading</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">To improve the execution efficiency of the program appropriately</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Can appropriately improve resource utilization (<span style="line-height:normal; font-family:Helvetica">CPU</span>, Memory Utilization)</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Disadvantages of multithreading</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Open threads need to occupy a certain amount of memory (by default, The main thread takes up <span style="line-height:normal; font-family:Helvetica">1M</span>, the child threads occupy <span style="line-height:normal; font-family:Helvetica">512KB</span>), If you open a large number of threads, will consume a lot of memory space, reduce the performance of the program</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">The more threads, the greater the overhead of the<span style="line-height:normal; font-family:Helvetica">CPU</span> on the dispatch thread</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Programming is more complex: such as communication between threads, multi-threaded data sharing</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><span style="line-height:normal; font-family:Helvetica">4.</span> Multithreading in the development of <span style="line-height:normal; font-family:Helvetica">IOS</span> application</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Main thread <span style="line-height:normal; font-family:Helvetica">:</span> after <span style="line-height:normal; font-family:Helvetica">an IOS</span> program runs, The default is to turn on <span style="line-height:normal; font-family:Helvetica">1</span> threads, called <span style="line-height:normal; font-family:Helvetica">the "</span> main thread <span style="line-height:normal; font-family:Helvetica">"</span> or <span style="line-height:normal; font-family:Helvetica">"UI</span> thread <span style="line-height:normal; font-family:Helvetica">"</span></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Main functions of the primary thread</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Display <span style="line-height:normal; font-family:Helvetica">\</span> Refresh <span style="line-height:normal; font-family:Helvetica">UI</span> Interface</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:46px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Handling <span style="line-height:normal; font-family:Helvetica">UI</span> events (such as click events, Scrolling events, drag events, etc.)</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"></p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Use of the main thread note <span style="line-height:normal; font-family:Helvetica">:</span> do not put more time-consuming operations into the main thread.</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Time-consuming operations can get stuck in the main thread, seriously affecting the smoothness of the <span style="line-height:normal; font-family:Helvetica">UI</span> , giving users a <span style="line-height:normal; font-family:Helvetica">"</span> card <span style="line-height:normal; font-family:Helvetica">"</span> bad Experience</p></p><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; margin-left:23px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"></p></p><span style="font-weight:normal"><span style="font-weight:normal"><span style="font-size:18px">5</span></span></span><span style="font-size:14px"><span style="font-size:14px"><strong>Thread's life cycle (thread State)</strong></span></span><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(1) New</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Instantiating a Thread object</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(2) Ready</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)">-(void) start;</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Sends a <span style="line-height:normal; font-family:Helvetica">start</span> message to the thread object, and the <span style="line-height:normal; font-family:Helvetica"></span> thread object is joined <span style="line-height:normal; font-family:Helvetica"></span> to a scheduled thread pool waiting for <span style="line-height:normal; font-family:Helvetica">CPU</span> scheduling</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><span style="line-height:normal; font-family:‘PingFang SC‘">The Detachnewthreadselector method and the</span> Performselectorinbackground <span style="line-height:normal; font-family:‘PingFang SC‘">method instantiate a thread object directly and join</span> the <span style="line-height:normal; font-family:‘PingFang SC‘">scheduler thread pool</span></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(3) operation</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">the <span style="line-height:normal; font-family:Helvetica">CPU</span> is responsible for scheduling the execution of threads in a scheduled thread pool</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Before the thread execution completes <span style="line-height:normal; font-family:Helvetica">(</span> before death <span style="line-height:normal; font-family:Helvetica">)</span>, the state may switch back and forth between ready and running</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">State changes between ready and running are <span style="line-height:normal; font-family:Helvetica">CPU</span> -responsible and programmers cannot intervene</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(4) Blocking</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">When a predetermined condition is met, you can use hibernation or lock blocking threads to execute</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)">+ (void) sleepuntildate: (nsdate *) date;</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)">+ (void) sleepfortimeinterval: (nstimeinterval) ti;</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)">@synchronized (self)<span style="line-height:normal; font-family:‘PingFang SC‘">: Exclusive lock Death</span> : + (void) exit</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(5) normal death</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Thread execution Complete</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">(6) Non-normal death</p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">When a condition is met, the thread inside itself aborts execution <span style="line-height:normal; font-family:Helvetica">(</span> suicide <span style="line-height:normal; font-family:Helvetica">), [nsthread exit];</span></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">When a condition is met, the thread of Death <span style="line-height:normal; font-family:Helvetica">(</span> the next decree) is given to other threads on the main thread <span style="line-height:normal; font-family:Helvetica">,</span> allowing the child thread to break itself <span style="line-height:normal; font-family:Helvetica">.</span> Be forced to die <span style="line-height:normal; font-family:Helvetica">)</span></p></p><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)"><p style="margin-top:0px; margin-bottom:0px; line-height:normal; font-family:‘PingFang SC‘; color:rgb(69,69,69)">Note <span style="line-height:normal; font-family:Helvetica">:</span> before terminating the thread, you should pay attention to releasing the previously allocated object <span style="line-height:normal; font-family:Helvetica">!</span></p></p><p style="margin-top:8px; margin-bottom:8px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><p style="margin-top:8px; margin-bottom:8px; line-height:normal; font-family:Helvetica; color:rgb(69,69,69)"><br></p></p><br><p><p>iOS development multithreading-related Concepts of multithreading (1)</p></p></span>