美文网首页
Dispatch For OC

Dispatch For OC

作者: fordG | 来源:发表于2018-10-10 09:02 被阅读0次
#import "ViewController.h"
#import "TestModel.h"

#define Log NSLog(@"%s", __FUNCTION__)
#define LogN(n) NSLog(@"%@", n)

@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//同步队列阻塞主线程

//异步队列: 串行队列和并发队列, 默认开启子线程执行队列里面加进去的任务

TestModel *model = [TestModel new];

model.name = @"h";

LogN(model.name);

}

- (IBAction)buttonTap:(UIButton *)sender {

//    [self globalQueue];

//    [self serialQueue];

//    [self conCurrentQueue];

//    [self groupQueue];

//    [self barrier];

[self semaphore];

}

- (void)functionOne{

Log;

}

- (void)functionSecond{

Log;

}

- (void)functionThree{

Log;

}

- (void)functionFour{

Log;

}

- (void)test {

NSString *a = @"123";

NSString *b = @"456";

NSString *c = [NSString stringWithFormat:@"%@%@", a, b];

LogN(a);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

LogN(c);

});

}

- (void)globalQueue{

/*

全局队列 并发队列

identifier: DISPATCH_QUEUE_PRIORITY_HIGH        高优先级

DISPATCH_QUEUE_DEFAULT    默认优先级

DISPATCH_QUEUE_LOW        低优先级

DISPATCH_QUEUE_BACKGROUND  后台模式(在所有高优先级都被添加到执行队列并且系统将在后台的当前Qqueue执行任务才被添加执行)

高优先级和默认优先级队列在执行异步操作时没有先后顺序, 然后低优先级, 最后后台模式

*/

__weak typeof(self)weakSelf = self;

dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_queue_t globalQueue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

dispatch_queue_t globalQueue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_queue_t globalQueue4 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);

dispatch_async(globalQueue2, ^{

[weakSelf functionSecond];

});

dispatch_async(globalQueue3, ^{

[weakSelf functionThree];

});

dispatch_async(globalQueue4, ^{

[weakSelf functionFour];

});

dispatch_async(globalQueue, ^{

[weakSelf functionOne];

});

}

- (void)serialQueue{

/*

异步串行队列

label:一个依附于对别的字符串标签

attr:预先定义的队列类型  DISPATHC_QUEUE_SERIAL串行队列

串行队列提交的任务会按照提交顺序依次执行

*/

__weak typeof(self)weakSelf = self;

const char *label = "serialQueue";

dispatch_queue_t serialQueue = dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{

[weakSelf functionOne];

[weakSelf functionSecond];

[weakSelf functionThree];

dispatch_async(serialQueue, ^{

[weakSelf functionOne];

});

});

dispatch_async(serialQueue, ^{

[weakSelf functionFour];

});

}

- (void)conCurrentQueue{

/*

并发队列

DISPATCH_QUEUE_CONCURRENT 并发队列

异步提交到并发队列的任务执行顺序具有不可预测性,因为并发队列在执行异步任务时会创建多条子线程,耗时较少的会先执行

*/

const char *label= "conCurrentQueue";

dispatch_queue_t conCurrentQueue = dispatch_queue_create(label, DISPATCH_QUEUE_CONCURRENT);

__weak typeof(self)weakSelf = self;

dispatch_async(conCurrentQueue, ^{

[weakSelf test];

});

dispatch_async(conCurrentQueue, ^{

[weakSelf functionOne];

});

}

- (void)groupQueue{

/*

任务操作组 dispatch_group

group中如果还有异步操作,就不能监听了

*/

dispatch_group_t groupQueue = dispatch_group_create();

dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

__weak typeof(self)weakSelf = self;

dispatch_group_async(groupQueue, globalQueue, ^{

[weakSelf functionOne];

});

dispatch_group_async(groupQueue, globalQueue, ^{

[weakSelf test];

});

dispatch_group_async(groupQueue, globalQueue, ^{

[weakSelf functionThree];

});

dispatch_group_notify(groupQueue, globalQueue, ^{

dispatch_async(dispatch_get_main_queue(), ^{

LogN(@"任务组执行完成");

});

});

}

- (void)barrier{

/*

dispatch_barrier 用于实现在当前队列同步执行一个block,主要用于多个异步任务并发的分段处理, barrier操作的队列加入到队列时,会默认执行完成前面加进去的block, 在单独执行此block

*/

const char *label = "conCurrentQueue";

dispatch_queue_t conCurrentQueue = dispatch_queue_create(label, DISPATCH_QUEUE_CONCURRENT);

__weak typeof(self)weakSelf = self;

dispatch_async(conCurrentQueue, ^{

[weakSelf functionOne];

});

dispatch_async(conCurrentQueue, ^{

[weakSelf functionSecond];

});

dispatch_barrier_async(conCurrentQueue, ^{

[weakSelf test];

});

dispatch_async(conCurrentQueue, ^{

[weakSelf functionThree];

});

dispatch_async(conCurrentQueue, ^{

[weakSelf functionFour];

});

}

- (void)semaphore{

/*

dispatch_semaphore 控制执行顺序

*/

dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_queue_t queue1 = dispatch_queue_create("1", DISPATCH_QUEUE_CONCURRENT);

dispatch_queue_t queue2 = dispatch_queue_create("2", DISPATCH_QUEUE_CONCURRENT);

dispatch_queue_t queue3 = dispatch_queue_create("3", DISPATCH_QUEUE_CONCURRENT);

dispatch_semaphore_t semaphore1 = dispatch_semaphore_create(1);

dispatch_semaphore_t semaphore2 = dispatch_semaphore_create(2);

__weak typeof(self)weakSelf = self;

dispatch_async(globalQueue, ^{

dispatch_async(queue1, ^{

[weakSelf functionOne];

dispatch_semaphore_signal(semaphore1); //发送信号

});

dispatch_semaphore_wait(semaphore1, DISPATCH_TIME_FOREVER);

dispatch_async(queue2, ^{

[weakSelf test];

dispatch_semaphore_signal(semaphore2);

});

dispatch_semaphore_wait(semaphore2, DISPATCH_TIME_FOREVER);

dispatch_async(queue3, ^{
    [weakSelf functionSecond];
});
});

}

@end

相关文章

网友评论

      本文标题:Dispatch For OC

      本文链接:https://www.haomeiwen.com/subject/onguaftx.html