-(void) Viewdidload {
[Super Viewdidload];
/*gcd:grand Central Dispatch, a hub scheduler that automatically manages the lifecycle of a thread (creating a scheduled destroy). When a task is stored in a queue, GCD automatically takes the task out of the queue, first-in and out, and in the thread.
Synchronous execution of tasks: Executing dispatch_sync in the current thread (dispatch_queue_t queue,dispatch_block_t block)
Execute tasks asynchronously: Executing Dispatch_async (...) on other threads
Queue dispatch_queue_t:
Concurrent queues (enabling multiple threads to perform multiple tasks, only valid for asynchronous execution): provided by default, no need to manually create global concurrency queue Dispatch_get_global_queue (dispatch_queue_priority_t priority,0);
Serial queue: Create Dispatch_queue_creat (' name ', property null)
Main queue (main thread associated) Dispatch_get_main_queue ()
It depends on how you perform the task, not opening a new thread.
*/
/*
The primary queue is the serial queue associated with the main thread, which is executed only on the main thread, regardless of how it is executed, and is generally asynchronous.
The child thread takes the data and the main thread shows. Go to the main thread way: 1. Go directly to 2. Put it on the home team joins
*/
/*
Deferred execution: 1. Call NSObject [self performselector:withobject:afterdelay:2.0]; The method is called on which thread, then the deferred execution method is executed on that thread.
2. Use GCD dispatch_after (dispatch_time_t when,dispatch_queue_t queue,dispatch_block_t block), and when to perform this task on this queue. (The home row is on the main thread, and the concurrent queue is new)
Example: Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec)), queue, ^{
*/
/*
Implement one-time code: dispatch_once function
Example: Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Execute code only 1 times (this is thread-safe by default)
});
The entire program runs, only once.
*/
/*
Queue group: You can have two tasks at the same time, exact two tasks are complete, then do other things. Asynchronous to do a few time-consuming things, waiting to go back to the main thread when the queue group.
Create a group of dispatch_group_t groups = Dispatch_group_create ();
Open a Task Dispatch_group_async (group, global concurrency queue, ^{});
Turn on other tasks Dispatch_group_async (group, global concurrency queue, ^{})
Perform All Tasks
When finished, return to the main thread dispatch_group_notify (group, main thread, ^{});
*/
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Dispatch_async (Queue, ^{
NSString *imagestrring = [NSString stringwithformat:@ "http://i6.topit.me/6/5d/45/1131907198420455d6o.jpg"];
Nsurl *imageurl = [Nsurl urlwithstring:imagestrring];
NSData *imagedata = [NSData Datawithcontentsofurl:imageurl];
Self.image = [UIImage imagewithdata:imagedata];
Dispatch_async (Dispatch_get_main_queue (), ^{
Self.imageView.image = Self.image;
});
});
UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
Button.frame = CGRectMake (40, 40, 100, 100);
[Button settitle:@ "click" Forstate:uicontrolstatenormal];
[Button Setbackgroundcolor:[uicolor Redcolor];
[Button addtarget:self action: @selector (clicked) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:button];
}
-(void) clicked
{
Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (40, 140, 300, 300)];
Imageview.image = Self.image;
[Self.view Addsubview:imageview];
}
iOS development multi-thread GCD