IOS GCD (Serial, concurrent basic use of threads)

Source: Internet
Author: User

General function names with Create\copy\new\retain and other words, need to use this data when the release// GCD data type in the ARC environment does not need to do release// The data type of the CF (Core Foundation) still needs to be release in the ARC environment    
@implementationHmviewcontroller- (void) viewdidload{[Super Viewdidload];    [Self performselectorinbackground: @selector (Test) Withobject:nil]; //[self syncmainqueue];}- (void) test{NSLog (@"Test---%@", [Nsthread CurrentThread]); Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{NSLog (@"Task---%@", [Nsthread CurrentThread]); });} /** * Use Dispatch_async async functions to add tasks to the main queue    in the main thread */-(void ) Asyncmainqueue {    //1. Get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); //2. Add a task to the queue to executeDispatch_async (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]); });} /** * Use the Dispatch_sync synchronization function to add a task to the primary queue in the main thread: The task cannot be executed down  /-(void  ) Syncmainqueue {    //1. Get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); //2. Add a task to the queue to executeDispatch_sync (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]); });//dispatch_sync (queue, ^{//NSLog (@ "----Download Picture 2-----%@", [Nsthread CurrentThread]);//    });//dispatch_sync (queue, ^{//NSLog (@ "----Download Picture 3-----%@", [Nsthread CurrentThread]);//    }); //does not open a new thread, all tasks are executed in the main thread}//all functions, such as create\copy\new\retain, need to be release when the data is not needed.//GCD data types do not need to be release in the ARC environment//The data type of the CF (Core Foundation) still needs to be release in the ARC environment /** * Use Dispatch_sync sync function to add a task to the string  /-(void  ) Syncserialqueue {    //1. Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("Com.itheima.queue", NULL); //2. Add a task to the queue to executeDispatch_sync (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"----Download Image 2-----%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"----Download Image 3-----%@", [Nsthread CurrentThread]);        }); //3. Releasing Resources//dispatch_release (queue); //MRC (non-ARC)//Summary: New threads are not opened} /** * Use Dispatch_sync synchronization function to add tasks to the concurrent queue  */-(void  ) Syncglobalqueue {     //  1. Get the global concurrent queue dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_        DEFAULT, 0); //2. Add a task to the queue to executeDispatch_sync (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"----Download Image 2-----%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"----Download Image 3-----%@", [Nsthread CurrentThread]);        }); //Summary: New threads are not opened and concurrent queues lose concurrency} /** * Add tasks to the serial queue with the Dispatch_async async function  */-(void  ) Asyncserialqueue {     //  1. Create serial queue dispatch_queue_t queue = dispatch_queue_create ("com.itheima.queue         ", NULL); //2. Add a task to the queue to executeDispatch_async (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"----Download Image 2-----%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"----Download Image 3-----%@", [Nsthread CurrentThread]);        }); //Summary: Execute a task with only 1 threads open} /** * Add task to concurrent queue with Dispatch_async async function  */-(void  ) Asyncglobalqueue {     //  1. Get the global concurrent queue dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_        DEFAULT, 0); //2. Add a task to the queue to executeDispatch_async (Queue, ^{NSLog (@"----Download Image 1-----%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"----Download Image 2-----%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"----Download Image 3-----%@", [Nsthread CurrentThread]);        }); //Summary: 3 threads Open at the same time}@end

IOS GCD (Serial, concurrent basic use of threads)

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.