Network Multithreading for iOS development 1. pthred (POSIX) General situation no 2. Nsthread
- nsthread *thread1 = [[nsthread alloc] initwithtarget: Self Selector:@selector(dealthreadexit:) Object:nil];
- [Thread1 start];
- [nsthread detachnewthreadselector:@selector(detailtextlabel) totarget: Self withobject:nil];
-  
- [[nsnotificationcenter defaultcenter] addobserver:self selector:@ Selector(dealthreadexit:) name:nsthreadwillexitnotification object:nil];
- [Thread1 cancel]; // call thread1 to stop.
- [Thread1 iscancelled]; // See if Thread1 can stop
- [nsthread exit]; // Stop current thread
- [Self performselectoronmainthread:<# (SEL) #> withobject:<# (ID) #> waituntildone:<# (BOOL) #>] let this method execute in the main thread
3.NSOperation
------------------Nsoperationqueue----------
Execute now
Nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];
[Operationqueue addoperationwithblock:^{
}];
Set the maximum number of concurrent threads for a thread pool
Operationqueue.maxconcurrentoperationcount = 2;
Created a thread that can be put into a thread pool
Nsinvocationoperation *inth1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (thread1:) object : nil];
Set the priority of the thread that is put into the thread pool
inth1.queuepriority = 1;
[Operationqueue addoperation:inth1];
[Nsthread ismainthread];//whether the current thread is the main path
[Nsthread ismultithreaded]; Whether the current thread is
[Nsthread sleepfortimeinterval:1.0]; The current thread sleeps xx seconds
4.GCD
Create a priority task
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Start an async asynchronous task
Dispatch_async (Queue, ^{
for (int i=0; i<20; i++) {
NSLog (@ "A =%d", i);
}
});
Start an asynchronous task again
Dispatch_async (Queue, ^{
for (int i=0; i<20; i++) {
NSLog (@ "B =%d", i);
}
});
GCD the simplest way to open an asynchronous task//create an asynchronous thread incoming priority
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
});
Open a task to execute on the main thread
Dispatch_async (Dispatch_get_main_queue (), ^{
});
Open an asynchronous task to execute only once
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
});
Open an asynchronous task Group XXX can control what tasks are performed after all tasks are completed
Open an asynchronous task to execute after xx seconds
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
});
Group Task Group is a task group that can add multiple tasks to him
dispatch_group_t group = Dispatch_group_create ();
Add Task 7s Finish/Task group//Priority Settings/
Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
});
10s complete
Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
});
These tasks are all performed asynchronously by this method to detect when a task group group does not have a task that is being performed .... Task
Dispatch_group_notify (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
NSLog (@ "All tasks performed, Auto Power off");
});
Network Multithreading for iOS development