iOS??????С??
???????????? ???????[ 2015/7/23 9:45:22 ] ??????????????? iOS
??????iOS???????????????????????????????????(async??sync)??????????(?????У????ж???)????????????????
????1.??????????
????*????????????????
????[self performSelectorInBackground:@selector(test) withObject:nil];
????*???????????????
????[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
????2.GCD ?? ???????????
????1> ????????
* ??????
* ???????????????е????????????У?????У??????????????
* dispatch_get_global_queue
* ???ж???
* ???????????ж????е?????????????У??????????
* dispatch_queue_create("myqueue"?? 0);
* ??????
* ???????????????е??????????????????е?(??????????й??)
* dispatch_get_main_queue
????2> ?????????????????????(??????????У?????????С????ж???)
????* ?????dispatch_sync???????????????????????μ????
????* ????dispatch_async???????????????????????μ????
????3.NSOperationNSOperationQueue
????1> ??ò???
????* ????NSOperation
????* ???NSOperation??NSOperationQueue
????2> ???
* ???????????
* ???????????? maxConcurrentOperationCount
* ???????Operation?????????? addDependency
*/
#import "XMQViewController.h"
@interface XMQViewController ()
{
NSOperationQueue *_queue;
}
@end
@implementation XMQViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_queue = [[NSOperationQueue alloc] init];
// ??????????????????3?????
_queue.maxConcurrentOperationCount = 3;
// ????????
// [self test];
// ??????
// [self gcd_group];
// GCD???????
// [self gcd_barrier];
// ??????
[self gcd_apply];
// ??????
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT?? 0);
// ???ж???
// dispatch_queue_t queue2 = dispatch_queue_create("myqueue"?? 0);
dispatch_async(queue?? ^{ // ???????
NSLog(@"????????????---%@"?? [NSThread currentThread]);
});
}
- (IBAction)click
{
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operation---%@"?? [NSThread currentThread]);
}];
NSOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operation2---%@"?? [NSThread currentThread]);
}];
// ?÷????????????????
// [operation start];
// ??????л???????????
[_queue addOperation:operation];
[_queue addOperation:operation2];
// ??????????? (operation2??????operation??operation???????????????operation2)
[operation addDependency:operation2];
}
// ????????????
- (IBAction)click2
{
// ??????
// dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT?? 0);
// ???ж???
dispatch_queue_t queue2 = dispatch_queue_create("myqueue"?? 0);
double delayInSeconds = 5.0;
dispatch_time_t delayInNanoSeconds =dispatch_time(DISPATCH_TIME_NOW?? delayInSeconds * NSEC_PER_SEC);
dispatch_after(delayInNanoSeconds?? queue2?? ^{
NSLog(@"??????");
});
}
// dispatch_group_async(??????)????????????????????????????????????????????(????????????????????UI??)
- (void)gcd_group
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT?? 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group?? queue?? ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"group1");
});
dispatch_group_async(group?? queue?? ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"group2");
});
dispatch_group_async(group?? queue?? ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"group3");
});
dispatch_group_notify(group?? dispatch_get_main_queue()?? ^{
NSLog(@"updateUI");
});
}
// dispatch_barrier_async ??????????????н????????У?????????????????????????????????
- (void)gcd_barrier
{
dispatch_queue_t queue = dispatch_queue_create("myqueue"?? DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue?? ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"dispatch_async1");
});
dispatch_async(queue?? ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"dispatch_async2");
});
dispatch_barrier_async(queue?? ^{
NSLog(@"dispatch_barrier_async");
[NSThread sleepForTimeInterval:4];
});
dispatch_async(queue?? ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"dispatch_async3");
});
}
// dispatch_apply ?????????????N??
- (void)gcd_apply
{
dispatch_queue_t queue = dispatch_queue_create("myqueue"?? DISPATCH_QUEUE_PRIORITY_DEFAULT);
dispatch_apply(10?? queue?? ^(size_t index) {
NSLog(@"??????10??");
});
}
// ????????
- (void)test
{
UIImage *image = nil;
[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue()?? ^{
NSLog(@"????UI????---%@"?? [NSThread currentThread]);
});
}
@end
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11