1 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
2 dispatch_async(queue, ^{
3 dispatch_group_t group = dispatch_group_create();
4 dispatch_group_async(group, queue, ^{
5
6 });
7 dispatch_group_async(group, queue, ^{
8
9 });
10 dispatch_group_notify(group, queue, ^{
11 dispatch_async(dispatch_get_main_queue(), ^{
12
13 });
14 });
15 });
1 //串行队列
2 dispatch_queue_t serialQueue = dispatch_queue_create("myThreadQueue1", DISPATCH_QUEUE_SERIAL);
3 //并发队列
4 dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
5 for (int i = 0; i < count; ++i) {
6 // 异步加载
7 dispatch_async(serialQueue, ^{
8 [self loadImage:[NSNumber numberWithInt:i]];
9 });
10 // 同步加载
11 dispatch_sync(serialQueue, ^{
12 [self loadImage:[NSNumber numberWithInt:i]];
13 });
14 }
15
16
17 dispatch_barrier_async(globalQueue, ^{
18 [self loadImage:[NSNumber numberWithInt:count - 1]];
19 });
20 for (int i = 0; i < count - 1; ++i) {
21 dispatch_async(globalQueue, ^{
22 [self loadImage:[NSNumber numberWithInt:i]];
23 });
24 }
1 dispatch_queue_t mainQueue = dispatch_get_main_queue();
2 dispatch_sync(mainQueue, ^{
3 [self updateImage:imageData];
4 });
5
6 dispatch_async(mainQueue, ^{
7 [self updateImage:imageData];
8 });
1 1、Thread
2 for (int i = 0; i < ROW_COUNT * COLUMN_COUNT; ++i) {
3 [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:[NSNumber numberWithInt:i]];
4 NSThread * thread=[[NSThread alloc] initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
5 thread.name=[NSString stringWithFormat:@"myThread%i",i];
6 [thread start];
7 }
8
9
10
11
12
13 [self performSelectorOnMainThread:@selector(updateImage:) withObject:imageData waitUntilDone:YES];
14 [self performSelectorInBackground:@selector(updateImage:) withObject:imageData];
1 NSOperationQueue * operationQueue = [[NSOperationQueue alloc] init];
2 operationQueue.maxConcurrentOperationCount = 5;
3 for (int i = 0; i < count; ++i) {
4 [operationQueue addOperationWithBlock:^{
5 [self loadImage:[NSNumber numberWithInt:i]];
6 }];
7 }
8
9 依赖关系
10 NSBlockOperation * lastBlockOperation= [NSBlockOperation blockOperationWithBlock:^{
11 [self loadImage:[NSNumber numberWithInt:(count - 1)]];
12 }];
13 for (int i = 0; i < count - 1; ++i) {
14 NSBlockOperation * blockOperation = [NSBlockOperation blockOperationWithBlock:^{
15 [self loadImage:[NSNumber numberWithInt:i]];
16 }];
17 [blockOperation addDependency:lastBlockOperation];
18 [operationQueue addOperation:blockOperation];
19 }
20
21 [operationQueue addOperation:lastBlockOperation];
22
23
24
25
26 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
27 [self updateImage:imageData];
28 }];