-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,482 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// QSDispatchQueue.h | ||
// | ||
// Created by zhongpingjiang on 2017/7/21. | ||
// Copyright © 2017年 shaoqing. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface QSDispatchQueue : NSObject | ||
|
||
#pragma mark - main queue + global queue | ||
|
||
/** | ||
全局并发队列的最大并发数,默认4 | ||
*/ | ||
+ (QSDispatchQueue *)mainThreadQueue; | ||
|
||
+ (QSDispatchQueue *)defaultGlobalQueue; | ||
|
||
+ (QSDispatchQueue *)lowGlobalQueue; | ||
|
||
+ (QSDispatchQueue *)highGlobalQueue; | ||
|
||
+ (QSDispatchQueue *)backGroundGlobalQueue; | ||
|
||
|
||
#pragma mark - | ||
|
||
@property (nonatomic,assign,readonly)NSUInteger concurrentCount; | ||
|
||
- (instancetype)init; | ||
|
||
/** | ||
默认最大并发数是1 | ||
@param queue 创建的并发队列 | ||
*/ | ||
- (instancetype)initWithQueue:(dispatch_queue_t)queue; | ||
|
||
|
||
/** | ||
@param queue 创建的并发队列 | ||
@param concurrentCount 最大并发数,应大于1 | ||
*/ | ||
- (instancetype)initWithQueue:(dispatch_queue_t)queue | ||
concurrentCount:(NSUInteger)concurrentCount; | ||
|
||
|
||
/** | ||
同步,并行队列变成了串行队列,最大并发数只能是1,设置concurrentCount是无效的 | ||
@param block 任务block | ||
*/ | ||
- (void)sync:(dispatch_block_t)block; | ||
|
||
|
||
/** | ||
异步 | ||
@param block 任务block | ||
*/ | ||
- (void)async:(dispatch_block_t)block; | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// | ||
// QSDispatchQueue.m | ||
// | ||
// Created by zhongpingjiang on 2017/7/21. | ||
// Copyright © 2017年 shaoqing. All rights reserved. | ||
// | ||
|
||
#import "QSDispatchQueue.h" | ||
|
||
static const NSUInteger kDefaultConcurrentCount = 1; //默认/最小并发数 | ||
static const NSUInteger kGlobalConcurrentCount = 4; //默认全局队列线程并发数 | ||
static const NSUInteger kMaxConcurrentCount = 32; //最大并发数 | ||
|
||
@interface QSDispatchQueue() | ||
|
||
@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 QSDispatchQueue | ||
|
||
#pragma mark - main queue + global queue | ||
+ (QSDispatchQueue *)mainThreadQueue { | ||
static QSDispatchQueue *queue; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken,^{ | ||
queue = [[QSDispatchQueue alloc] initWithQueue:dispatch_get_main_queue()]; | ||
}); | ||
|
||
return queue; | ||
} | ||
|
||
+ (QSDispatchQueue *)defaultGlobalQueue{ | ||
|
||
static QSDispatchQueue *queue; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
queue = [[QSDispatchQueue alloc]initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | ||
concurrentCount:kGlobalConcurrentCount]; | ||
}); | ||
return queue; | ||
} | ||
|
||
+ (QSDispatchQueue *)lowGlobalQueue{ | ||
|
||
static QSDispatchQueue *queue; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
queue = [[QSDispatchQueue alloc]initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) | ||
concurrentCount:kGlobalConcurrentCount]; | ||
}); | ||
return queue; | ||
} | ||
|
||
+ (QSDispatchQueue *)highGlobalQueue{ | ||
|
||
static QSDispatchQueue *queue; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
queue = [[QSDispatchQueue alloc]initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) | ||
concurrentCount:kGlobalConcurrentCount]; | ||
}); | ||
return queue; | ||
} | ||
|
||
+ (QSDispatchQueue *)backGroundGlobalQueue{ | ||
|
||
static QSDispatchQueue *queue; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
queue = [[QSDispatchQueue alloc]initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) | ||
concurrentCount:kGlobalConcurrentCount]; | ||
}); | ||
return queue; | ||
} | ||
|
||
#pragma mark - lifycycle | ||
- (instancetype)init{ | ||
|
||
return [self initWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | ||
concurrentCount: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 = MIN(concurrentCount, kMaxConcurrentCount); | ||
if (concurrentCount < kDefaultConcurrentCount) { | ||
concurrentCount = kDefaultConcurrentCount; | ||
} | ||
|
||
_concurrentCount = concurrentCount; //concurrentCount在[kDefaultConcurrentCount,kMaxConcurrentCount]之间 | ||
if (!_semaphore) { | ||
_semaphore = dispatch_semaphore_create(concurrentCount); | ||
|
||
} | ||
if (!_serialQueue) { | ||
_serialQueue = dispatch_queue_create([[NSString stringWithFormat:@"com.buaa.nanhua.serial_%p", self] UTF8String], DISPATCH_QUEUE_SERIAL); | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark -- sync && async | ||
|
||
//同步 | ||
- (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(_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 |
Oops, something went wrong.