Cat Learn iOS (54) nsoperation important knowledge of multi-threaded network

Source: Internet
Author: User
<span id="Label3"></p><p><p>Cat share, must Boutique</p></p><p><p>Original articles, Welcome Reprint. Reprint please specify: Sanayu's blog<br>Address: http://blog.csdn.net/u013357243?viewmode=contents</p></p>One: queue type and queue Add task 1: primary queue <ul> <ul> <li>[nsoperationqueue mainqueue]</li> <li>Actions added to the "home column" will be executed in the main thread.</li> </ul> </ul>2: Non-home Row <ul> <ul> <li>[[nsoperationqueue alloc] init]</li> <li>Actions added to the "non-home column" will be executed in the SUB-THREAD.</li> </ul> </ul>3: Queue Add Task <ul> <ul> <li> <ul> <li>(void) addoperation: (nsoperation *) op;</li> </ul></li> <li> <ul> <li>(void) addoperationwithblock: (void (^) (VOID)) block;</li> </ul></li> </ul> </ul>Two: common usage 1: set the maximum number of concurrent<p><p>(1) concurrent Number: The number of tasks at the same time. for example, 3 threads are executing 3 tasks at the same time, and the number of concurrency is 3.<br>(2) Max Concurrency: The maximum number of tasks that can be performed at the same time.<br>(3) the most significant concurrent number of the relevant? method.<br>-(nsinteger) maxconcurrentoperationcount;<br>-(void) setmaxconcurrentoperationcount: (nsinteger) cnt;<br>note: if the maximum concurrency number is not set, then the number of concurrent is determined by the system memory and cpu, may be a little more memory, less memory to open less.<br>Note: the value of NUM does not represent the number of threads, only the ID of the THREAD.<br>Tip: the maximum number of concurrent do not write (less than 5), do not open too much, generally to both of the appropriate, because although the task is processed on the child thread, but the CPU processing these excessive child threads may affect the ui, let the UI Card.</p></p><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *) event{<span class="hljs-comment"><span class="hljs-comment">//1. Create a queue (not a home Row)</span></span>Nsoperationqueue *queue = [[nsoperationqueue alloc] init];<span class="hljs-comment"><span class="hljs-comment">//2. Set maximum concurrency (up to 2 simultaneous tasks Concurrently)</span></span>Queue<span class="hljs-variable"><span class="hljs-variable">. Maxconcurrentoperationcount</span></span>=<span class="hljs-number"><span class="hljs-number">2</span></span>;<span class="hljs-comment"><span class="hljs-comment">//3. Add operations to the queue (automatically executes tasks asynchronously, concurrently)</span></span>Nsblockoperation *operation1 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 1---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation2 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 2---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation3 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 3---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation4 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 4---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsinvocationoperation *operation5 = [[nsinvocationoperation alloc] initwithtarget:<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span>Selector<span class="hljs-keyword"><span class="hljs-keyword">@selector</span></span>(download) object:<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>]; [queue addoperation:operation1]; [queue addoperation:operation2]; [queue addoperation:operation3]; [queue addoperation:operation4]; [queue addoperation:operation5]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 5---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 6---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 7---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 8---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 9---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }];}</code></pre><p><p>Effect: we set up 9 simulated download operation, at the same time to open the thread up to two, note that there are 2,3,4 three threads on the way, but we guarantee that the same time only two threads, 2 began to do things, and then did not do, for the thread 3, 4.<br></p></p>2: Other operations of the queue <ul> <ul> <li>Cancel all operations</li> <li>(void) cancelalloperations;</li> </ul> </ul><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *) event{<span class="hljs-comment"><span class="hljs-comment">//1. Create a queue (not a home Row)</span></span>Nsoperationqueue *queue = [[nsoperationqueue alloc] init];<span class="hljs-comment">//// <span class="hljs-comment">2. Set maximum concurrency (up to 2 tasks concurrently)</span></span><span class="hljs-comment"><span class="hljs-comment">//queue.maxconcurrentoperationcount = 2;</span></span> <span class="hljs-comment"><span class="hljs-comment">//3. Add operations to the queue (automatically executes tasks asynchronously, concurrently)</span></span>Nsblockoperation *operation1 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 1---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation2 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 2---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation3 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 3---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsblockoperation *operation4 = [nsblockoperation blockoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 4---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; Nsinvocationoperation *operation5 = [[nsinvocationoperation alloc] initwithtarget:<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span>Selector<span class="hljs-keyword"><span class="hljs-keyword">@selector</span></span>(download) object:<span class="hljs-literal"><span class="hljs-literal">Nil</span></span>]; [queue addoperation:operation1]; [queue addoperation:operation2]; [queue addoperation:operation3]; [queue addoperation:operation4]; [queue addoperation:operation5]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 5---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 6---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 7---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 8---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue addoperationwithblock:^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"download picture 9---%@"</span></span>, [<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>currentthread]); }]; [queue cancelalloperations]; }</code></pre><p><p>Result Diagram:<br><br>This usage is actually used in managing Memory.</p></p><p><p>That should Work.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java">- (<span class="hljs-keyword">void</span>)didReceiveMemoryWarning{ [<span class="hljs-keyword">super</span> didReceiveMemoryWarning]; <span class="hljs-comment">// 取消队列中的所有任务(不可恢复)</span>}</code></pre></pre> <ul> <ul> <li><p>Pause all operations<br>[queue setsuspended:yes];</p></li> <li><p>Restore all operations<br>[queue setsuspended:no];</p></li> </ul> </ul><p><p>Similar to canceling all operations, in practice, when our TableView interacts with the customer (scrolling) pauses, does not interact to continue execution</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec">- (<span class="hljs-keyword">void</span>)scrollViewWillBeginDragging:(<span class="hljs-built_in">UIScrollView</span> *)scrollView{<span class="hljs-comment">// [queue setSuspended:YES]; // 暂停队列中的所有任务</span>}- (<span class="hljs-keyword">void</span>)scrollViewDidEndDragging:(<span class="hljs-built_in">UIScrollView</span> *)scrollView willDecelerate:(<span class="hljs-built_in">BOOL</span>)decelerate{<span class="hljs-comment">// [queue setSuspended:NO]; // 恢复队列中的所有任务</span>}</code></pre></pre>Iii: dependencies between operations (interview QUESTIONS) <ul> <ul> <li>Dependencies can be set between nsoperation to guarantee the order of execution</li> <li>[OPERATIONB adddependency:operationa];<br>Operation b depends on Operation a, and operation A is performed before Operation B is Performed.</li> <li>Note: do not rely on each other, such as a dependent b,b dependency a</li> <li>You can create a dependency between nsoperation of different queue</li> </ul> </ul><p><p>The simple thing is to have the thread execute in order, which is an execution that relies on the execution of another thread.</p></p><pre class="prettyprint"><code class=" hljs java">-(<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *) event{<span class="hljs-javadoc"><span class="hljs-javadoc">/** assumes a, B, c three operations, requires: 1.3 operations are executed asynchronously 2. Operation C relies on Operation B 3. Operation b depends on Operation a/</span></span> <span class="hljs-comment"><span class="hljs-comment">//1. Create a queue (not a home Row)</span></span>Nsoperationqueue *queue = [[nsoperationqueue alloc] init];<span class="hljs-comment"><span class="hljs-comment">//2. Create 3 actions</span></span>Nsblockoperation *operationa = [nsblockoperation Blockoperationwithblock:^{NSLog (@<span class="hljs-string"><span class="hljs-string">"A---%@"</span></span>, [nsthread currentthread]); }]; Nsblockoperation *operationb = [nsblockoperation Blockoperationwithblock:^{NSLog (@<span class="hljs-string"><span class="hljs-string">"B---%@"</span></span>, [nsthread currentthread]); }]; Nsblockoperation *operationc = [nsblockoperation Blockoperationwithblock:^{NSLog (@<span class="hljs-string"><span class="hljs-string">"C---%@"</span></span>, [nsthread currentthread]); }];<span class="hljs-comment"><span class="hljs-comment">//set Dependencies</span></span>[OPERATIONB adddependency:operationa]; [operationc adddependency:operationb];<span class="hljs-comment"><span class="hljs-comment">//3. add actions to the queue (automatically execute tasks asynchronously)</span></span>[queue addoperation:operationc]; [queue addoperation:operationa]; [queue addoperation:operationb];}</code></pre><p><p>Effect<br></p></p>Four: Communication between threads<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cpp">NSOperationQueue *<span class="hljs-built_in">queue</span> = [[NSOperationQueue alloc] init];[<span class="hljs-built_in">queue</span> addOperationWithBlock:^{ <span class="hljs-comment">// 1.执行一些比较耗时的操作的代码</span> [[NSOperationQueue mainQueue] addOperationWithBlock:^{ <span class="hljs-comment">// 2.回到主线程操作的代码</span> }];}];</code></pre></pre><p><p>This back to the previous download image similar to the Case. This is done on the child thread and then back to the main thread.</p></p> <p><p>Cat Learn iOS (54) nsoperation important knowledge of multi-threaded network</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.