//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];