iOS Multithreading implementation types
Nsthread
Nsoperationqueue
NSObject
GCD
***************
1.NSThread
Threads
First Kind
Nsthread *thread1=[[nsthread alloc] initwithtarget:self selector: @selector (sum) object:nil];
//
give the thread a name
[Email protected] "thread1";
Start Thread
[Thread1 start];
End Thread
[Thread1 Cancel];
The second (does not need to be manually opened )
[Nsthread detachnewthreadselector: @selector (sum) totarget:self Withobject:nil];
2. two subcategories of nsoperation
// one .
Nsinvocationoperation *inop=[[nsinvocationoperation alloc] initwithtarget:self Selector :@selector(Invocation) object:nil];
[Inop start];
// two .
Nsblockoperation *blop=[nsblockoperation blockoperationwithblock: ^{
NSLog (@ " I am block");
}];
Create a queue
Nsoperationqueue *queue=[[nsoperationqueue alloc] init];
//Set maximum concurrent quantity
Queue.maxconcurrentoperationcount=2;
Add Event
[Queue Addoperation:inop];
[Queue addoperation:blop];
3.NSObject
[Self performselectorinbackground: @selector (sum) withobject:nil];
4.GCD
GCD (FIFO )
Serial : The previous task was completed before a task could be executed
Parallel : The task is ordered when it is dispatched , but should not begin until the first task is completed .
GCD queue in 3 : Primary queue , global queue , custom queue
1. Use the master queue for task dispatch (serial ), in the main thread
dispatch_queue_t Mainqueue=dispatch_get_main_queue ();
1.1 Adding tasks
Dispatch_async (Mainqueue, ^{
NSLog (@ " first task: current thread is%@", [Nsthread CurrentThread]);
});
Serial
2. Custom Queues
/*
dispatch_queue_t myqueue=dispatch_queue_create ("Com.lanlan.myqueue", dispatch_queue_serial);
2.1 Adding Tasks
Dispatch_async (Myqueue, ^{
NSLog (@ " first task: current thread:%@", [Nsthread CurrentThread]);
});
Parallel
3. Custom Queues
/*
dispatch_queue_t myqueue2=dispatch_queue_create ("Com.lanlan.myqueue", dispatch_queue_concurrent);
3.1 Adding tasks
Dispatch_async (MyQueue2, ^{
NSLog (@ " first task: current thread:%@", [Nsthread CurrentThread]);
});
4. Global Queue
/*
dispatch_queue_t globelqueue=dispatch_get_global_queue (dispatch_queue_priority_default, 0);
4.1 Adding Tasks
Dispatch_async (Globelqueue, ^{
NSLog (@ " first task: current thread:%@", [Nsthread CurrentThread]);
});
5. Only one execution is guaranteed
Guaranteed to execute only once
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
NSLog (@ " the code here only executes once");
});
6. Delay after 5 seconds to execute
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
NSLog (@ " wait for me 5 seconds ...");
});
7.// Repeat execution
Dispatch_apply (5, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^ (size_t t) {
NSLog (@ " nice weather Today");
});
Child threads
[Nsthread detachnewthreadselector:@selector(newthead) totarget:self withobject: Nil];
//back to main thread
[self performselectoronmainthread:@selector(Mymaintherad) withobject: Self Waituntildone:NO];
-(void) Mymaintherad
{
Print Thread
NSLog (@ " I am in the mainline thread %@", [Nsthread CurrentThread]);
}
Multithreading Nsthread GCD