//swift中的线程延时
let time: TimeInterval = 1.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
print("延时1秒执行")
}
//在主线程中延迟执行某动作,不会卡主主线程,不影响后面的东做执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@", [NSThread currentThread]);
});
//在子线程中执行某动作,不会卡主整个线程
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(@"%@", [NSThread currentThread]);
});
//实现延迟,该线程本身在哪个线程中就再哪个线程中执行
NSURL *url = [NSURL URLWithString:@"http://59320.jpg.com"];
[selfperformSelector:@selector(download:) withObject:url afterDelay:3];
//利用sleep实现延迟(不要用这个,会卡住主线程,即后面的动作不会执行)
[NSThread sleepForTimeInterval:3];