Multi-thread implementation solution in iOS and multi-thread solution in ios

Source: Internet
Author: User

Multi-thread implementation solution in iOS and multi-thread solution in ios

What is a main thread?

After an iOS program runs, a thread is enabled by default, which is called "main thread" or "UI thread"

 

Main functions of the main thread

1. Display/refresh the UI

2. Process UI events (such as click events, scroll events, and drag events)

 

Usage of the main thread

1. Do not place time-consuming operations in the main thread

2. Time-consuming operations will be stuck in the main thread, seriously affecting the smoothness of the UI

 

, Put time-consuming operations in the main thread, the task will be executed in the serial order, after clicking the button in the fifth second, the interface will be stuck for 5 seconds

Because the time-consuming operation has not been completed, you cannot immediately respond to the button clicking

 

 

1. Use of pthread

void *run(void *parme) {        NSLog(@"%@",[NSThread currentThread]);            for (int i = 0; i < 100000; i++) {        NSLog(@"%d",i);    }    return NULL;    }- (IBAction)btnClick:(id)sender {        pthread_t thread;    pthread_create(&thread, NULL, run, NULL);    }

 

2. Use of NSThread

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self createThread3];} // method for creating the first event-(void) createThread1 {// you need several threads to alloc several NSThread * threads = [[NSThread alloc] initWithTarget: self selector: @ selector (run :) object: @ "first"]; thread. name = @ "one_thread"; [thread start] ;}// method 2-(void) createThread2 {[NSThread detachNewThreadSelector: @ selector (run :) toTarget: self withObject: @ "second"];} // method 3-(void) createThread3 {[self defined mselectorinbackground: @ selector (run :) withObject: @ "third"];} -(void) run :( NSString *) param {NSLog (@ "______ % @ _____ % @", param, [NSThread currentThread]);}

 

3. Use of GCD

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {[self syncMain];} /** synchronization function + main column */-(void) syncMain {dispatch_queue_t queue = dispatch_get_main_queue (); NSLog (@ "syncMain ---- begin "); // Add the task to the queue dispatch_sync (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "2 ---- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "3 ---- % @", [NSThread currentThread]) ;}); NSLog (@ "syncMain ---- end") ;}/ ** asynchronous Function + main queue column */-(void) asyncMain {// asynchronous functions won't open threads when used in the main queue column // get the serial queue dispatch_queue_t queue = dispatch_get_main_queue (); // Add the task to the queue dispatch_async (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "2 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "3 ---- % @", [NSThread currentThread]);} /** synchronous Function + Serial queue: New threads are not enabled and tasks are executed in the current thread */-(void) syncSerial {// create a serial queue dispatch_queue_t queue = dispatch_queue_create ("com.520.queue", DISPATCH_QUEUE_SERIAL); // Add the task to the queue dispatch_sync (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "2 ---- % @", [NSThread currentThread]);}); dispatch_sync (queue, ^ {NSLog (@ "3 ---- % @", [NSThread currentThread]);} /** asynchronous Function + Serial queue: New threads are enabled, but tasks are serialized. After a task is executed, the next task is executed. */-(void) asyncSerial {// create a serial queue dispatch_queue_t queue = dispatch_queue_create ("com.5?queue", DISPATCH_QUEUE_SERIAL); // Add the task to the queue dispatch_async (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "2 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "3 ---- % @", [NSThread currentThread]);} /** synchronous Function + concurrent queue: no thread is enabled. */-(void) syncConcurrent {// obtain the global concurrent queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0 ); // Add the task to the queue dispatch_async (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "1 ---- % @", [NSThread currentThread]);}/** asynchronous Function + concurrent queue: Multiple Threads can be enabled at the same time */-(void) asycConcurrent {// create a queue // The first parameter is the tag equivalent to the name // The second parameter for serial or parallel transmission // dispatch_queue_t queue = dispatch_queue_create ("img. download ", DISPATCH_QUEUE_CONCURRENT); // obtain the global concurrent queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); // Add the task to the queue dispatch_async (queue, ^ {for (int I = 0; I <10; I ++) {NSLog (@ "1 ---- % @", [NSThread currentThread]) ;}}); // Add the task to the queue dispatch_async (queue, ^ {for (int I = 0; I <10; I ++) {NSLog (@ "2 ---- % @", [NSThread currentThread]) ;}}); // Add the task to the queue dispatch_async (queue, ^ {for (int I = 0; I <10; I ++) {NSLog (@ "3 ---- % @", [NSThread currentThread]) ;}});}View Code

When using GCD, there are two main steps:

1. Custom task

2. Add the task to the queue

 

It is also necessary to distinguish between synchronous, asynchronous, parallel, and serial.

Synchronous Asynchronization: determines whether to enable new threads.

Parallel serial: affects the execution of tasks.

 

Related Article

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.