1. sleepForTimeInterval,此函数会卡住当前线程,一般不用
[NSThread sleepForTimeInterval:3];
2. performSelector,定制好延迟任务后,不会卡主"当前线程"(3秒后运行download:方法)
[self performSelector:@selector(download:) withObject:@"http://555.jpg" afterDelay:3];
3.3秒后回到"主线程"运行block中的代码
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{ NSLog(@"------task------%@", [NSThread currentThread]);}); dispatch_queue_t queue = dispatch_get_main_queue();dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{ NSLog(@"------task------%@", [NSThread currentThread]);})
4.3秒后自己主动开启"新线程"运行block中的代码
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{ NSLog(@"------task------%@", [NSThread currentThread]);});