/**
* Create a queue (serial)
**/
dispatch_queue_t queue1 = dispatch_queue_create ("queue1", dispatch_queue_serial);
/**
* Add the path of the code block execution thread
**/
Dispatch_async (Queue1, ^{
for (int i = 0; i<100; i++) {
NSLog (@ "line Cheng,%d", i);
}
});
Create a thread in the form of a C-language function
Dispatch_sync_f (queue1, NULL, treadfun);//using thread functions as the path to the thread
void Treadfun () {
for (int i =0; i<100; i++) {
NSLog (@ "Thread 2,%d", i);
}
}
dispatch_time_t time = Dispatch_time (Dispatch_time_now, 2*nsec_per_sec);
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Dispatch_after (time, queue, ^{
NSLog (@ "222");
// });
The above code is equivalent to
NSLog (@ "11111");
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
NSLog (@ "2222222");
});
/**
* Executed after a period of time
1,dispatch_after
2,nsobject Performselector
3,timer Timer method
**/
[Super Viewdidload];
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:5.0 target:self selector: @selector (myfun) Userinfo:nil Repeats:yes];
[Timer invalidate];
[Self performselector: @selector (myfun) Withobject:nil afterdelay:5.0];
iOS Learning note 31-Multithreading in depth