//
// QDispatchQueue.h
// GCDPropertyQueue
//
// Created by maochengfang on 2020/10/28.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface QDispatchQueue : NSObject
+ (QDispatchQueue *)mainThreadQueue;
+ (QDispatchQueue *)defaultGlobalQueue;
+ (QDispatchQueue *)lowGlobalQueue;
+ (QDispatchQueue *)hightGlobalQueue;
+ (QDispatchQueue *)backGroundGlobalQueue;
@property (nonatomic, assign, readonly) NSUInteger concurrentCount;
- (instancetype)init;
- (instancetype)initWithQueue:(dispatch_queue_t)queue;
- (instancetype)initWithQueue:(dispatch_queue_t)queue concurrentCount:(NSUInteger)concurrentCount;
- (void)sync:(dispatch_block_t)block;
- (void)async:(dispatch_block_t)block;
@end
NS_ASSUME_NONNULL_END
//
// QDispatchQueue.m
// GCDPropertyQueue
//
// Created by maochengfang on 2020/10/28.
//
#import "QDispatchQueue.h"
static const NSUInteger kDefaultConcurrentCount = 1; //默认/最小并发数
static const NSUInteger kGlobalConcurrentCount = 4; //默认全局队列线程并发数
static const NSUInteger kMaxConcurrentCount = 32; //最大并发数
@interface QDispatchQueue ()
@property (nonatomic, strong) dispatch_queue_t serialQueue;
@property (nonatomic, strong) dispatch_semaphore_t semaphore;
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, assign) NSUInteger concurrentCount;
@end
@implementation QDispatchQueue
+ (QDispatchQueue *)mainThreadQueue{
static QDispatchQueue *queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[QDispatchQueue alloc] initWithQueue:dispatch_get_main_queue()];
});
return queue;
}
+ (QDispatchQueue *)lowGlobalQueue{
static QDispatchQueue *queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[QDispatchQueue alloc] initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) concurrentCount:(NSUInteger)kDefaultConcurrentCount];
});
return queue;
}
+ (QDispatchQueue *)hightGlobalQueue{
static QDispatchQueue *queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[QDispatchQueue alloc] initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) concurrentCount:(NSUInteger)kDefaultConcurrentCount];
});
return queue;
}
+ (QDispatchQueue *)backGroundGlobalQueue{
static QDispatchQueue * queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = [[QDispatchQueue alloc] initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) concurrentCount:(NSUInteger)kDefaultConcurrentCount];
});
return queue;
}
- (instancetype)init{
return [self initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) concurrentCount:(NSUInteger)kDefaultConcurrentCount];
}
- (instancetype)initWithQueue:(dispatch_queue_t)queue{
return [self initWithQueue:queue concurrentCount:kDefaultConcurrentCount];
}
- (instancetype)initWithQueue:(dispatch_queue_t)queue concurrentCount:(NSUInteger)concurrentCount{
self = [super init];
if(self){
if(!queue){
_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}else{
_queue = queue;
_concurrentCount = concurrentCount;
if(concurrentCount < kDefaultConcurrentCount){
concurrentCount = kDefaultConcurrentCount;
}
_concurrentCount = concurrentCount;
if(!_semaphore){
_semaphore = dispatch_semaphore_create(concurrentCount);
}
if(!_serialQueue){
_serialQueue = dispatch_queue_create([[NSString stringWithFormat:@"com.maozeze.nanhua.serial_%p",self] UTF8String], DISPATCH_QUEUE_SERIAL);
}
}
}
return self;
}
- (void)sync:(dispatch_block_t)block{
if(!block){
return;
}
dispatch_sync(_serialQueue, ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //semaphore -1
dispatch_sync(self.queue, ^{
if(block){
block();
}
dispatch_semaphore_signal(self.semaphore); //semaphore +1
});
});
}
- (void)async:(dispatch_block_t)block{
if(!block){
return;
}
dispatch_async(self.serialQueue, ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER); //semaphore -1
dispatch_async(self.queue, ^{
if(block){
block();
}
dispatch_semaphore_signal(self.semaphore); //semaphore +1
});
});
}
@end
测试异步和同步
- (void)testAsync{
dispatch_queue_t workConcurrentQueue = dispatch_queue_create("com.jzp.async.queue", DISPATCH_QUEUE_CONCURRENT);
QDispatchQueue *queue = [[QDispatchQueue alloc]initWithQueue:workConcurrentQueue concurrentCount:3];
for (NSInteger i = 0; i < 10; i++) {
[queue async:^{
NSLog(@"thread-info:%@开始执行任务%d",[NSThread currentThread],(int)i);
sleep(1);
NSLog(@"thread-info:%@结束执行任务%d",[NSThread currentThread],(int)i);
}];
}
NSLog(@"异步:主线程任务...");
}
- (void)testSync{
dispatch_queue_t workConcurrentQueue = dispatch_queue_create("com.jzp.sync.queue", DISPATCH_QUEUE_CONCURRENT);
QDispatchQueue *queue = [[QDispatchQueue alloc]initWithQueue:workConcurrentQueue concurrentCount:1];
for (NSInteger i = 0; i < 10; i++) {
[queue sync:^{
NSLog(@"thread-info:%@开始执行任务%d",[NSThread currentThread],(int)i);
sleep(1);
NSLog(@"thread-info:%@结束执行任务%d",[NSThread currentThread],(int)i);
}];
}
NSLog(@"异步:主线程任务...");
}
本文介绍了一个基于Grand Central Dispatch (GCD)的Objective-C并发队列类库——GCDPropertyQueue。该库提供了不同优先级的全局队列、后台队列及主线程队列等,并实现了异步和同步调度方法。
1515

被折叠的 条评论
为什么被折叠?



