When the iOS development thread operates, the generic method name determines whether the new thread (Async,sync) is turned on, and the queue type (global queue, serial queue) determines how many threads to open
1. Fast thread invocation
* Turn on background threads to perform tasks
[Self performselectorinbackground: @selector (Test) Withobject:nil];
* Go back to the main thread to perform the task
[Self Performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:yes];
2.GCD: Dependent queue execution
1> queue Type
* Global Queue
* All tasks added to the global queue are executed concurrently (concurrently, multiple threads may be turned on)
* Dispatch_get_global_queue
* Serial Queue
* All tasks added to the serial queue are executed sequentially (one thread is open)
* Dispatch_queue_create ("Myqueue", 0);
* Home Row
* All tasks added to the main queue are executed in the main thread (not related to the method name)
* Dispatch_get_main_queue
2> synchronous or asynchronous, depending on the method name (does not affect the home column, affects the global queue, serial queue)
* Sync: Dispatch_sync, perform task on current thread, not open new thread
* Async: Dispatch_async, performs tasks on other threads, opens new thread
3.nsoperation\nsoperationqueue
1> Use steps
* Create Nsoperation
* Add Nsoperation to Nsoperationqueue
2> Advantages
* More Object-oriented
* Can control the maximum number of concurrent Maxconcurrentoperationcount
* Add dependencies between tasks (operation) adddependency
*/
#import "XMQViewController.h"
@interface Xmqviewcontroller ()
{
Nsoperationqueue *_queue;
}
@end
@implementation Xmqviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
_queue = [[Nsoperationqueue alloc] init];
Control maximum concurrency and execute up to 3 threads at the same time
_queue.maxconcurrentoperationcount = 3;
Back to main thread
[Self test];
Thread Monitoring
[Self gcd_group];
GCD thread Dependency
[Self gcd_barrier];
Loop execution
[Self gcd_apply];
Global queue
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Serial queue
dispatch_queue_t queue2 = dispatch_queue_create ("Myqueue", 0);
Dispatch_async (queue, ^{//time-consuming operation
NSLog (@ "This is an asynchronous thread---%@", [Nsthread CurrentThread]);
});
}
-(ibaction) click
{
Nsoperation *operation = [Nsblockoperation blockoperationwithblock:^{
NSLog (@ "Operation---%@", [Nsthread CurrentThread]);
}];
Nsoperation *operation2 = [Nsblockoperation blockoperationwithblock:^{
NSLog (@ "Operation2---%@", [Nsthread CurrentThread]);
}];
This method is executed by default on the main thread
[Operation start];
The join queue executes on an asynchronous thread
[_queue addoperation:operation];
[_queue Addoperation:operation2];
Set thread dependency (Operation2 depends on operation,operation execution to execute operation2)
[Operation Adddependency:operation2];
}
Defer execution of a thread
-(Ibaction) Click2
{
Global queue
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Serial queue
dispatch_queue_t queue2 = dispatch_queue_create ("Myqueue", 0);
Double delayinseconds = 5.0;
dispatch_time_t delayinnanoseconds =dispatch_time (Dispatch_time_now, delayinseconds * NSEC_PER_SEC);
Dispatch_after (Delayinnanoseconds, Queue2, ^{
NSLog (@ "deferred execution");
});
}
Dispatch_group_async (asynchronous method) can be implemented to listen to a set of tasks are completed, and then be notified to perform other actions (such as notifying users to download, display UI, etc.)
-(void) Gcd_group
{
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
dispatch_group_t group = Dispatch_group_create ();
Dispatch_group_async (group, queue, ^{
[Nsthread sleepfortimeinterval:1];
NSLog (@ "group1");
});
Dispatch_group_async (group, queue, ^{
[Nsthread Sleepfortimeinterval:2];
NSLog (@ "group2");
});
Dispatch_group_async (group, queue, ^{
[Nsthread Sleepfortimeinterval:3];
NSLog (@ "Group3");
});
Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{
NSLog (@ "UpdateUI");
});
}
Dispatch_barrier_async is executed after the execution of the previous task is completed, and subsequent tasks must wait until it finishes executing
-(void) Gcd_barrier
{
dispatch_queue_t queue = dispatch_queue_create ("Myqueue", dispatch_queue_concurrent);
Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:2];
NSLog (@ "dispatch_async1");
});
Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:4];
NSLog (@ "DISPATCH_ASYNC2");
});
Dispatch_barrier_async (Queue, ^{
NSLog (@ "Dispatch_barrier_async");
[Nsthread Sleepfortimeinterval:4];
});
Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:2];
NSLog (@ "dispatch_async3");
});
}
Dispatch_apply executes a code fragment n times
-(void) gcd_apply
{
dispatch_queue_t queue = dispatch_queue_create ("Myqueue", Dispatch_queue_priority_default);
dispatch_apply, queue, ^ (size_t index) {
NSLog (@ "loop execution 10 times");
});
}
Back to main thread
-(void) test
{
UIImage *image = nil;
[Self Performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:yes];
Dispatch_async (Dispatch_get_main_queue (), ^{
NSLog (@ "Update UI interface---%@", [Nsthread CurrentThread]);
});
}
@end
iOS Thread Development Summary