#pragma mark - GCD队列的组合
//1.并发队列 + 同步执行:没有开启新线程,串行执行任务
- (void)syncConcurrent {
NSLog(@"---------syncConcurrent----start");
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"---------syncConcurrent----end");
}
//2.并发队列 + 异步执行:开启新线程,并发执行任务 (除了主线程,又开启了3个线程,并且任务是交替着同时执行的,任务不是马上执行,而是将所有任务添加到队列之后才开始同步执行)
- (void)asyncConcurrent {
NSLog(@"---------asyncConcurrent----start");
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(concurrentQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"---------asyncConcurrent----end");
}
//3.串行队列 + 同步执行:没有开启新线程,串行执行任务(所有任务都是在主线程中执行的,并没有开启新的线程。而且由于串行队列,所以按顺序一个一个执行。加入以后,立即执行)
- (void)syncSerial {
NSLog(@"------sycnSerial__start-----");
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"------sycnSerial__end-----");
}
//4.串行队列 + 异步执行:开启新的线程(1条),串行执行任务(开启了一条新线程,但是任务还是串行,所以任务是一个一个执行。任务不是马上执行,而是将所有任务添加到队列之后才开始同步执行)
- (void)asyncSerial {
NSLog(@"------asycnSerial__start-----");
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(serialQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"------asycnSerial__end-----");
}
//5.主队列 + 同步执行:没有开启新的线程,串行执行(程序卡死)
- (void)syncMain {
NSLog(@"-------sycnMain--start");
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_sync(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"-------sycnMain--end");
}
//6.主队列 + 异步执行:没有开启新的线程,串行执行(异步执行,具备开启线程的能力,但因为是主队列,所以所有任务都在主线程中,并且一个接一个执行。任务不是马上执行,而是将所有任务添加到队列之后才开始同步执行。)
- (void)asyncMain {
NSLog(@"-------asycnMain--start");
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"1:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"2:%d---%@", i, [NSThread currentThread]);
}
});
dispatch_async(mainQueue, ^{
for (int i = 0; i < 5; i++) {
NSLog(@"3:%d---%@", i, [NSThread currentThread]);
}
});
NSLog(@"-------asycnMain--end");
}
#pragma mark - 线程通讯
- (void)communicate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i < 5; i++) {
NSLog(@"%d---%@", i, [NSThread currentThread]);
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"主线程%@", [NSThread currentThread]);
});
});
}
#pragma mark - GCD栅栏方法
//执行完栅栏前面的操作之后,才执行栅栏操作,最后再执行栅栏后边的操作。
- (void)barrier {
dispatch_queue_t concurrentQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
NSLog(@"----1-----%@", [NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"----2-----%@", [NSThread currentThread]);
});
dispatch_barrier_async(concurrentQueue, ^{
NSLog(@"----barrier-----%@", [NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"----3-----%@", [NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"----4-----%@", [NSThread currentThread]);
});
}
#pragma mark - GCD延迟执行
- (void)after {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"----after----");
});
}
#pragma mark - GCD ONCE
- (void)dispatchOnce {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"只执行一次的代码");
});
}
#pragma mark - GCD的快速迭代方法
- (void)apply {
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(6, globalQueue, ^(size_t index) {
NSLog(@"%zd-----%@", index, [NSThread currentThread]);
});
}
#pragma mark - GCD的队列组
//有时候我们会有这样的需求:分别异步执行2个耗时操作,然后当2个耗时操作都执行完毕后再回到主线程执行操作。这时候我们可以用到GCD的队列组。
//我们可以先把任务放到队列中,然后将队列放入队列组中。
//调用队列组的dispatch_group_notify回到主线程执行操作。
- (void)group {
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"第一个耗时长的异步操作");
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"第二个耗时长的异步操作");
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"第三个耗时长的异步操作");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"所有耗时长的操作执行完成,回到主线程");
});
}
NSOperation
#pragma mark - NSOperation (NSInvocationOperation)
//在没有使用NSOperationQueue、单独使用NSBlockOperation的情况下,NSBlockOperation也是在主线程执行操作,并没有开启新线程
- (void)invocationOperation {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
[operation start];
}
- (void)run {
NSLog(@"NSInvocationOperation: ---- %@", [NSThread currentThread]);
}
#pragma mark - NSOperation (NSBlockOperation)
//blockOperationWithBlock:方法中的操作是在主线程中执行的,
//而addExecutionBlock:方法中的操作是在其他线程中执行的
- (void)blockOperation {
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1------%@", [NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"2------%@", [NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"3------%@", [NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"4------%@", [NSThread currentThread]);
}];
[operation start];
}
#pragma mark - NSOperationQueue
- (void)operationQueue {
//获取主队列
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
//其他队列
// 添加到这种队列中的任务(NSOperation),就会自动放到子线程中执行
// 同时包含了:串行、并发功能
NSOperationQueue *otherQueue = [[NSOperationQueue alloc] init];
}
#pragma mark - OperationQueue
//NSOperation需要配合NSOperationQueue来实现多线程。
//NSInvocationOperation和NSOperationQueue结合后能够开启新线程,进行并发执行NSBlockOperation和NSOperationQueue也能够开启新线程,进行并发执行
- (void)addOperationQueue {
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//2.创建操作
NSInvocationOperation *invoOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2) object:nil];
invoOperation.name = @"invoOperation";
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 3; ++i) {
NSLog(@"1-----%@", [NSThread currentThread]);
}
}];
blockOperation.name = @"blockOperation";
//3.添加到队列中
[queue addOperation:invoOperation];
[queue addOperation:blockOperation];
}
- (void)run2 {
for (int i = 0; i < 3; ++i) {
NSLog(@"2-----%@", [NSThread currentThread]);
}
}
#pragma mark - NSOperationQueue Block
//无需先创建任务,在block中添加任务,直接将任务block加入到队列中。
- (void)addOperationWithBlockToQueue {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
for (int i = 0; i < 9; ++i) {
NSLog(@"1-----%@", [NSThread currentThread]);
}
}];
[queue addOperationWithBlock:^{
for (int i = 0; i < 9; ++i) {
NSLog(@"2-----%@", [NSThread currentThread]);
}
}];
}
#pragma mark - 串行和并行
//maxConcurrentOperationCount 最大并发数量,默认为-1,表示不进行限制
//maxConcurrentOperationCount == 1 串行执行
//maxConcurrentOperationCount > 1 并行执行,很大时候系统自动调整
//当最大并发数为1时,任务是按顺序串行执行的。当最大并发数为2时,任务是并发执行的。而且开启线程数量是由系统决定的,不需要我们来管理
- (void)maxConcurrentOperationCount
{
// 创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 设置最大并发操作数
// queue.maxConcurrentOperationCount = 2;
queue.maxConcurrentOperationCount = 3; // 就变成了串行队列
// 添加操作
[queue addOperationWithBlock:^{
NSLog(@"1-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
[queue addOperationWithBlock:^{
NSLog(@"2-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
[queue addOperationWithBlock:^{
NSLog(@"3-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
[queue addOperationWithBlock:^{
NSLog(@"4-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
[queue addOperationWithBlock:^{
NSLog(@"5-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
[queue addOperationWithBlock:^{
NSLog(@"6-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.01];
}];
}
#pragma mark - 依赖操作
- (void)addDependency {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1-----%@", [NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2-----%@", [NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3-----%@", [NSThread currentThread]);
// [op2 cancel];
}];
NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"4-----%@", [NSThread currentThread]);
[queue cancelAllOperations];
}];
//op1 依赖于op3 ,只有op3结束以后才能执行op1
[op1 addDependency:op3];
[op2 addDependency:op4];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
}