Multithreading
Note: iOS refreshes and additions to the UI must operate in the main thread!
How to create a pthread:
pthread_t pthread; // first parameter thread pointer // Some properties of the second parameter thread // The third parameter function pointer is used to execute a method // pass-through values in the fourth parameter thread Pthread_create (&pthread, NULL, run, NULL);
When the application is just running, the system will automatically open a thread for us, which is the main thread.
A child thread is a thread that programmers manually open with code, and it is meant to perform tasks that are time-consuming .
First, the creation method of Nsthread:
1.
Object:@ "123"]; @" 123 " ; // Open Thread [Thread start];
2. Quick Create
[Nsthread detachnewthreadselector: @selector (Haoshicaozuo) totarget:self withobject:@ "456" ]; [Self Performselectorinbackground: @selector (Haoshicaozuo) Withobject: @" 123 "];
The following line of code refers to the amount of sleep time that is modified according to the specific needs.
[Nsthread sleepfortimeinterval:0.5];
Second, nsoperation
Nsoperation is an abstract class, we generally do not use it directly, but use its subclasses nsinvocationoperation and nsblockoperation, if they are used alone are executed in the main thread, Executes on a child thread only when it is used with the queue.
Nsinvocationoperation *operation1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( OperationAction1)Object: nil];//[Operation1 start];Nsinvocationoperation *operation2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( OperationAction2)Object: nil]; Nsblockoperation*operation3 = [Nsblockoperation blockoperationwithblock:^{ for(inti = -; I < -; i++) {NSLog (@"%d", i); } }]; //Join to queue//Mainqueue represents the home row//nsoperationqueue *queue = [Nsoperationqueue mainqueue]; //if it's Alloc init, that represents the other columns.//The first addition of the first execution, after the addition of the execution, but the execution of the time is not necessarily, may be executed after the execution of the first executionNsoperationqueue *queue =[[Nsoperationqueue alloc] init]; [Queue Addoperation:operation1]; [Queue Addoperation:operation2]; [Queue Addoperation:operation3];
Third, GCD
Async: Not executing on a thread
Synchronization: Executing on the same thread
Serial: strings are executed together
Parallel: Execute together
Sync + Home Column: All tasks submitted through GCD to the home column must be asynchronous, otherwise it will cause a deadlock
Async + Home column: does not open thread, it executes on the main thread (the home row is serial)
Synchronous + Serial-to-column: does not have the ability to open threads, completing tasks on the current thread
Async + Serial: Has the ability to open threads, but the task is serial, executing one before going to the next
Synchronous + parallel Queue: Without the ability to turn on threads, concurrent functions are useless.
Two ways to create a parallel queue:
dispatch_queue_t queue = dispatch_queue_create ("sb.2b.com"0);
Asynchronous + concurrent Queues
Three (one): GCD network request:
- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.dispatch_queue_t Queue= Dispatch_queue_create ("AAA", dispatch_queue_concurrent); Dispatch_async (Queue,^{Nsurl*url = [Nsurl urlwithstring:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec =1464140268&di=6b6b2e3ea5da34b7da1e02fd28c7acd2&src=http://pic36.nipic.com/20131115/12106414_ 153522431000_2.jpg"]; NSData*data =[NSData Datawithcontentsofurl:url]; Dispatch_sync (Dispatch_get_main_queue (),^{self.myImageView.image=[UIImage Imagewithdata:data]; }); }); }
Three (ii): Use of the GCD function:
Delay execution of block
void Dispatch_after (dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block);
To repeat the block, it is important to note that this method is returned in a synchronous manner, that is, until all blocks have been executed before returning, and if an asynchronous return is required, it is nested in Dispatch_async. Whether multiple blocks run concurrently or serially is also dependent on whether the queue is concurrent or serial.
void Dispatch_apply (size_t iterations, dispatch_queue_t queue, Void (^block) (size_t));
iOS Advanced Multi-threading