1.自創(chuàng)隊列(GCD)
dispatch_queue_t queue= dispatch_queue_create("wendingding", NULL);
dispatch_sync(queue, ^{31 NSLog(@"下載圖片1----%@",[NSThread currentThread]);32 });
2.同步
dispatch_async(dispatch_get_main_queue(), ^{
});
3.異步
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_sync(queue ^{
});
NSOperation 1.同步
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; //主隊列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //自定義隊列 NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ //任務(wù)執(zhí)行 }]; [queue addOperation:operation];
2.異步
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^(){
NSLog(@"執(zhí)行第1次操作年柠,線程:%@", [NSThread currentThread]);
}];
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^(){
NSLog(@"執(zhí)行第2次操作,線程:%@", [NSThread currentThread]);
}];
// operation1依賴于operation2
[operation1 addDependency:operation2];
[queue addOperation:operation1];
[queue addOperation:operation2];