Gcd-grand Central Dispath multithreaded ui_21 in iOS

Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">Gcd:grand Central Dispath "the great hub scheduler", written in pure c, provides a number of more powerful functions</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">GCD: Advantages</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">1. Now is the Apple main push thread management mode</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">2. It will automatically take advantage of more CPU resources (dual-core, Quad-core)</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">3. It automatically manages the life cycle of threads (creation/dispatch/destruction of threads);</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘">4. Programmers only need to tell gcd what tasks they want to perform, without writing a line of thread-managed code</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import "ViewController.h" #define KURLSTRING1 @ "http://www.nbsheji.cn/uploadfiles/2010113143922418.jpg" #define KURLString2 @ "http://amuse.nen.com.cn/imagelist/11/21/9as70n3ir61b.jpg" @interface viewcontroller () @property ( retain, nonatomic) iboutlet uiimageview *firstview;//first picture @property (retain, Nonatomic) iboutlet Uiimageview * Secondview;//second picture @property (nonatomic,retain) Nsmutablearray *datasource;//store requested data @end@implementation Viewcontroller//lazy Load-(nsmutablearray *) datasource{ if (_datasource = = Nil) { Self.datasource = [nsmutablearray arraywithcapacity:0]; } return [[_datasource retain]autorelease];}</pre></pre><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><span style="color:#ff0000">Serial queue: (thread Synchronization) The task that is added to this queue executes one after the other (one task is completed before another task is completed)</span></p></p><br><pre name="code" class="objc">-(ibaction) handleserialqueue: (UIButton *) sender {//get System serial queue//(1) add asynchronous task to the System's created serial queue, or complete it in the main thread; (2) Adding a synchronization task to a serial queue created by the system causes the thread to deadlock, causing the other person to be unable to execute; dispatch_queue_t queue1 = Dispatch_get_main_queue (); 01: the unique identity of the queue, in the form of an anti-domain name//02: the Queue's attribute type, which is to identify whether the queue is a serial or parallel queue//(1) to create a serial queue to add the asynchronous task is to complete the task in the child thread; (2) Adding a synchronization task to the serial queue created by itself is to complete the task in the main thread; dispatch_queue_t queue2 = dispatch_queue_create ("com.xcqnzf.xuchang", dispatch_queue_serial); /*//asynchronous Task//first parameter: task added to queue Name//second parameter: block execution task content Dispatch_async (queue2, ^{NSLog (@ "task 1%@%d", [nsthread Currentthread],[nsthread currentthread].ismainthread); }); Dispatch_async (queue2, ^{NSLog (@ "task 2%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Dispatch_async (queue2, ^{NSLog (@ "task 3%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Release your own created queue, create will release Dispatch_release (queue2); *///sync Task Dispatch_synC (queue2, ^{NSLog (@ "task 1%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Dispatch_sync (queue2, ^{NSLog (@ "task 2%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Dispatch_sync (queue2, ^{NSLog (@ "task 3%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Summary: synchronization task: Regardless of which queue is executed in the main thread, but cannot be added to the System's own serial queue;//asynchronous task: in a serial queue that you create, execute in a child thread, if the system-created queue executes in the main thread;}</pre><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><span style="color:#ff0000">2. Parallel Queue (thread Concurrency) The task added to this queue performs the illusion at the same time</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><span style="color:#ff0000"></span></p></p><pre name="code" class="objc">-(ibaction) handleconcurrentqueue: (UIButton *) sender {//1. Gets the parallel queue//01 that the system comes with. priority//02 for queues. reserved parameters for 0 Dispatch_queu e_t queue1 = Dispatch_get_global_queue (dispatch_queue_priority_default, 0); 2. Create a parallel queue yourself (generally do not create a parallel queue yourself, the System's parallel queue is enough) dispatch_queue_t queue2 = dispatch_queue_create ("com.xcqnzf.xuchang", dispatch_queue_concurrent); Sync task Dispatch_sync (queue1, ^{NSLog (@ "sync task%@%d", [nsthread currentthread],[nsthread Currentthread].ismainthre ad); }); Asynchronous task Dispatch_async (queue2, ^{NSLog (@ "task 1%@%d", [nsthread currentthread],[nsthread Currentthread].ismainthrea d); }); Dispatch_async (queue2, ^{NSLog (@ "task 2%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); }); Dispatch_async (queue2, ^{NSLog (@ "task 3%@%d", [nsthread currentthread],[nsthread currentthread].ismainthread); Communication between Threads//child thread back to main thread//get system serial queue Dispatch_async (dispatch_get_main_queue (), ^{NSLOG (@ "i'm back on the main thread"); }); }); Release appears create will release Dispatch_release (queue2); }</pre><br><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><span style="color:#ff0000">3. Group Queue: add multiple tasks to a group to execute, this will automatically send a pass after all tasks are completed</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><span style="color:#ff0000">Dispatch_group_notifity receives this message and then processes it after all tasks have been completed.</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:20px; font-family:‘STHeiti Light‘"></p></p><pre name="code" class="objc">-(ibaction) handlegroupqueue: (UIButton *) Sender {//1. Creating a parallel queue and performing tasks dispatch_queue_t queue = Dispatch_get_global_queu E (dispatch_queue_priority_default, 0); __block typeof (self) weakself = self; Create a group asynchronous synchronization task dispatch_group_t group = dispatch_group_create (); 01. Group name//02. The queue name to be added//03. task to perform dispatch_group_async (group, queue, ^{nsurl *urlstring = [nsurl urlwithstring:kurlstring1]; NSData *data1 = [nsdata datawithcontentsofurl:urlstring]; Storing the requested data using an array [weakself.datasource addobject:data1]; }); Dispatch_group_async (group, queue, ^{nsurl *url = [nsurl urlwithstring:kurlstring2]; NSData *data2 = [nsdata datawithcontentsofurl:url]; [weakself.datasource addobject:data2]; }); The following methods are automatically triggered when the tasks in the group are complete dispatch_group_notify (group, queue, ^{weakSelf.FirstView.image = [UIImage Imagew ithdata:weakself.datasource[0]]; WeakSelf.secondView.image = [UIImage Imagewithdata:weakself.datasource[1]]; }); Release grouping Dispatch_release (group);}</pre><br><span style="font-size:18px; color:#ff0000">To be <span style="font-size:18px; color:#ff0000">continued</span> ...</span><p><p></p></p><br><br><br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Gcd-grand Central Dispath multithreaded ui_21 in iOS</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.