1迷守、延遲函數(shù)
//延遲執(zhí)行
//[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(task) userInfo:nil repeats:YES];
//[self performSelector:@selector(task) withObject:nil afterDelay:3.0];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//GCD延遲執(zhí)行
/*
第一個參數(shù):表示從什么時候開始計時 DISPATCH_TIME_NOW:現(xiàn)在
第二個參數(shù):間隔的時間
第三個參數(shù):隊列,決定block在哪個線程中調(diào)用,只有當(dāng)隊列是主隊列的時候才在主線程調(diào)用
第四個參數(shù):
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{
NSLog(@"----GCD---%@",[NSThread currentThread]);
});
2期升、柵欄函數(shù)
//1.創(chuàng)建并發(fā)隊列
dispatch_queue_t queue = dispatch_queue_create("www.yifuj.com", DISPATCH_QUEUE_CONCURRENT);
//2.使用異步函數(shù)添加任務(wù)
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download 1--%zd-%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download 2--%zd-%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download 3--%zd-%@",i,[NSThread currentThread]);
}
});
//柵欄函數(shù):控制隊列中任務(wù)的執(zhí)行順序,前面的所有任務(wù)執(zhí)行完畢之后執(zhí)行柵欄函數(shù),自己執(zhí)行完畢之后再之后后面的任務(wù)
dispatch_barrier_async(queue, ^{
NSLog(@"++++++++++++++++++++++++++");
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download 4--%zd-%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download 5--%zd-%@",i,[NSThread currentThread]);
}
});